【云哥专刊】。。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;
 }
 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值