【雲哥專刊】。。Utils

 

  private static final DateFormat longDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 private static final DateFormat shortDateFormat = new SimpleDateFormat("yyyy-MM-dd");
 private static final DateFormat compactDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
 private static final DateFormat simpleDateFormat = new SimpleDateFormat("yyMM");
 private static final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

 /**
  * 返回當前日期,表現形式:yyyy-MM-dd HH:mm:ss
  * @return
  */
 public static String formatLongDate() {
  return longDateFormat.format(new Date());
 }

 /**
  * 返回當前日期,表現形式:yyyy-MM-dd
  * @return
  */
 public static String formatShortDate() {
  return shortDateFormat.format(new Date());
 }

 /**
  * 返回當前日期,表現形式:yyyyMMddHHmmss
  * @return
  */
 public static String formatCompactDate() {
  return compactDateFormat.format(new Date());
 }

 /**
  * 返回當前日期,表現形式:yyyyMM
  * @return
  */
 public static String formatSimpleDate() {
  return simpleDateFormat.format(new Date());
 }

 /**
  * 判斷對象非空或 null 包括 Collection、Map、String.
  * @param o
  * @return
  */
 @SuppressWarnings("unchecked")
 public static boolean isNotNullOrEmpty(Object o) {
  if (o == null)
   return false;
  if (o instanceof String) {
   String str = (String) o;
   if (str.trim().equals(""))
    return false;
  } else if (o instanceof Collection) {
   Collection c = (Collection) o;
   return (c != null && c.size() != 0);
  } else if (o instanceof Map) {
   Map map = (Map) o;
   return (map != null && map.size() != 0);
  }
  return true;
 }

 public static boolean isNullorEmpty(Object o) {
  if (o == null)
   return true;
  if (o instanceof String) {
   String str = (String) o;
   if (str.trim().equals(""))
    return true;
  } else if (o instanceof Collection) {
   Collection c = (Collection) o;
   return !(c != null && c.size() != 0);
  } else if (o instanceof Map) {
   Map map = (Map) o;
   return !(map != null && map.size() != 0);
  }
  return false;
 }

 /*
  * 當前日期 向前或向後指定日期。入參 0,0,0 則返回當前日期字符串 20090909000000
  */
 public static String getDate(Integer yearUp, Integer monthUp, Integer dateUp) {
  Calendar calendar = Calendar.getInstance();
  if (Utils.isNotNullOrEmpty(yearUp)) {
   calendar.add(Calendar.YEAR, yearUp);
  }
  if (Utils.isNotNullOrEmpty(monthUp)) {
   calendar.add(Calendar.MONTH, monthUp);
  }
  if (Utils.isNotNullOrEmpty(dateUp)) {
   calendar.add(Calendar.DATE, dateUp);
  }
  return shortDateFormat.format(calendar.getTime());
 }

 /**
  *  當前日期 向前或向後指定日期。入參 0,0,0 則返回當前日期字符串 2009-09-09
  * @param yearUp
  * @param monthUp
  * @param dateUp
  * @return 最後修改:Mar 22, 2011 3:40:19 PM 郭林
  */
 public static String getDate2(Integer yearUp, Integer monthUp, Integer dateUp) {
  Calendar calendar = Calendar.getInstance();
  if (Utils.isNotNullOrEmpty(yearUp)) {
   calendar.add(Calendar.YEAR, yearUp);
  }
  if (Utils.isNotNullOrEmpty(monthUp)) {
   calendar.add(Calendar.MONTH, monthUp);
  }
  if (Utils.isNotNullOrEmpty(dateUp)) {
   calendar.add(Calendar.DATE, dateUp);
  }
  return dateFormat.format(calendar.getTime());
 }

 /**
  * properties 使用
  */
 public void writeResourseProperty(String taskCode) {
  //寫入文件。
  Properties pp = new Properties();
  InputStreamReader isr = null;
  isr = new InputStreamReader(new Utils().getClass().getClassLoader().getResourceAsStream("international/SkyResources_zh_CN.properties"));
  try {
   pp.load(isr);
   if (Utils.isNotNullOrEmpty(taskCode)) {
    if (taskCode.indexOf("D") > -1) {
     pp.setProperty("dpaMaxTaskCodeD", taskCode);
    }
   }
   String path = this.getRequest().getSession().getServletContext().getRealPath(
     "/WEB-INF/classes/international/SkyResources_zh_CN.properties");
   pp.store(new FileOutputStream(path), "set");
   isr.close();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }

 /**
  * 打印時候查找判斷所需文件是否存在,如果不存在則創建出來
  *  void
  * 2011-1-12  上午09:40:13  郭林
  */
 public static void createFile() {
  String filePath1 = "C:/DocWord/myPrint.txt"; // 默認路徑
  String filePath2 = "C:/DocWord/print.doc"; // 默認路徑
  String filePath3 = "C:/DocWord/myPrint2.txt"; // 默認路徑
  File file1 = new File(filePath1);
  File file2 = new File(filePath2);
  File file3 = new File(filePath3);
  if (!file1.exists()) { //判斷文件是否存在。不存在則新創建
   try {
    file1.createNewFile();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
  if (!file2.exists()) { //判斷文件是否存在。不存在則新創建
   try {
    file2.createNewFile();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
  if (!file3.exists()) { //判斷文件是否存在。不存在則新創建
   try {
    file3.createNewFile();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 }
 /**
  * 獲取配置的存放文件的路徑
  * @param type
  * @return String
  * 2011-8-8  下午04:41:14  郭林
  */
 public static String getConfigFilePath(String type){
  String filePath = null;// 當前數據文件存放的路徑
  Properties pp = new Properties();
  InputStream in = Utils.class.getResourceAsStream("/filepath.properties");
  try {
   pp.load(in);
   filePath = pp.getProperty(type);
  } catch (IOException e) {
   e.printStackTrace();
   System.err.println("filepath.properties文件中的"+type+"屬性是否存在??");
  }
  return filePath;
 }
 
 /**
  * 獲取設備到期提醒天數
  * @return String
  * 2011-9-19  郭林
  */
 public static String getWarnDays(int type){
  Properties pp = new Properties();
  InputStream in = Utils.class.getResourceAsStream("/filepath.properties");
  try{
   pp.load(in);
  }catch(IOException e){
   System.err.println("filepath.properties文件中的是否存在??");
  }
  if(type == 0){
   return pp.getProperty("WarnDays");
  }else if(type == 1){
   return pp.getProperty("CheckWarn");
  }else{
   return null;
  }
 }
 
 /**
  * 當數據庫記錄刪除時,刪除上傳的文件
  * @param fullFilePath
  * @return boolean
  * 2011-8-8  下午04:50:06  郭林
  */
 public static boolean deleteUploadFile(String fullFilePath){
  boolean flag = false;
  File file = new File(fullFilePath);
  if (file.isFile() && file.exists()) {
   if (file.delete()) {
    flag=true;
    //log.info("對應文件已成功刪除。。。");
   }
  }
  return flag;
 }
 
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章