工作中经常遇到处理日期的问题, 当然有一些优秀开源的库(例如 joda-time, 功能十分强大), 不过个人还是比较偏好自写一点常用的代码, 毕竟开源库中我们实际使用到的功能并不多,如果引入库,个人感觉造成一些资源浪费(纯属个人观点)。下面就是我常用一个工具类,DateUtils, 仅仅就封装了一些本人工作中常用到的方法,这儿贴出来,代码如有不当之处,麻烦指出(不胜感激):
/** * @author ying.dong * DateUtils */public class DateUtils { private static final ThreadLocal<SimpleDateFormat> threadLocal = new ThreadLocal<SimpleDateFormat>(); /** * define datePattern * @author ying.dong * */ public enum DatePattern { DATE_TIME_PATTERN("yyyy-MM-dd HH:mm:ss"), DATE_PATTERN("yyyy-MM-dd"), DATE_NO_SYMBOL_PATTERN("yyyyMMdd"); private String datePattern; private DatePattern(String pattern) { this.datePattern = pattern; } public String getDatePattern() { return this.datePattern; } } /** * define operation unit * @author ying.dong * */ public enum OperationUnit { YEAR, MONTH, DAY, HOUR, MINUTE, SECOND; } /** * get SimpleDateFormat instance by ThreadLocal * @return */ private static SimpleDateFormat getSimpleDateFormat() { SimpleDateFormat formatter = threadLocal.get(); if (formatter == null) { formatter = new SimpleDateFormat(); threadLocal.set(formatter); } return formatter; } /** * config DateFomatter * @param datePattern */ private static SimpleDateFormat configFormatterPattern(DatePattern datePattern) { SimpleDateFormat formatter = getSimpleDateFormat(); formatter.applyPattern(datePattern.getDatePattern()); return formatter; } /** * parse calendar to String * @param calendar * @return */ public static String parseDate2Str(Calendar calendar) { return parseDate2Str(DatePattern.DATE_TIME_PATTERN, calendar.getTime()); } /** * parse date to String * @param date * @return */ public static String parseDate2Str(Date date) { return parseDate2Str(DatePattern.DATE_TIME_PATTERN, date); } /** * parse calendar to String * @param datePattern * @param calendar * @return String */ public static String parseDate2Str(DatePattern datePattern, Calendar calendar) { return parseDate2Str(datePattern, calendar.getTime()); } /** * parse Date to String * @param datePattern * @param date * @return String */ public static String parseDate2Str(DatePattern datePattern, Date date) { SimpleDateFormat formatter = configFormatterPattern(datePattern); return formatter.format(date); } /** * parse String to Date * @param datePattern * @param dateStr * @return Date * @throws ParseException */ public static Date parseStr2Date(DatePattern datePattern, String dateStr) throws ParseException { SimpleDateFormat formatter = configFormatterPattern(datePattern); return formatter.parse(dateStr); } /** * parse timestamp String to date * @param timestampStr * @return */ public static Date parseTimestampStr2Date(String timestampStr) { long timestamp = Long.parseLong(timestampStr) * 1000; return parseTimestampStr2Date(timestamp); } /** * parse timestamp to date * @param timestamp * @return */ public static Date parseTimestampStr2Date(long timestamp) { Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(timestamp * 1000); return calendar.getTime(); } /** * calendar plus operation * @param unit * @param amount * @param calendar * @return */ public static Calendar plus(OperationUnit unit, int amount, Calendar calendar) { switch (unit) { case YEAR: calendar.add(Calendar.YEAR, amount); break; case MONTH: calendar.add(Calendar.MONTH, amount); break; case DAY: calendar.add(Calendar.DAY_OF_YEAR, amount); break; case HOUR: calendar.add(Calendar.HOUR_OF_DAY, amount); break; case MINUTE: calendar.add(Calendar.MINUTE, amount); break; case SECOND: calendar.add(Calendar.SECOND, amount); break; default: break; } return calendar; } /** * date plus operation * @param unit * @param amount * @param date * @return */ public static Date plus(OperationUnit unit, int amount, Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar = plus(unit, amount, calendar); return calendar.getTime(); } /** * date plus operation with timestamp * @param date * @param timestamp * @return */ public static Date plus(Date date, long timestamp) { timestamp = date.getTime() + timestamp * 1000; date.setTime(timestamp); return date; } /** * calendar minus operation * @param unit * @param amount * @param calendar * @return */ public static Calendar minus(OperationUnit unit, int amount, Calendar calendar) { return plus(unit, -amount, calendar); } /** * date minus operation * @param unit * @param amount * @param date * @return */ public static Date minus(OperationUnit unit, int amount, Date date) { return plus(unit, -amount, date); } /** * date minus operation with timestamp * @param date * @param timestamp * @return */ public static Date minus(Date date, long timestamp) { return plus(date, -timestamp); } /** * whether the date is before when * @param date * @param when * @return * @throws ParseException */ public static boolean isBefore(Date date, String when) throws ParseException { Date whenDate = parseStr2Date(DatePattern.DATE_TIME_PATTERN, when); return isBefore(date, whenDate); } /** * whether the date is before when * @param date * @param when * @param datePattern * @return * @throws ParseException */ public static boolean isBefore(Date date, String when, DatePattern datePattern) throws ParseException { Date whenDate = parseStr2Date(datePattern, when); return isBefore(date, whenDate); } /** * whether the date is before when * @param date * @param when * @return */ public static boolean isBefore(Date date, Date when) { return date.before(when); } /** * whether the date is after when * @param date * @param when * @return * @throws ParseException */ public static boolean isAfter(Date date, String when) throws ParseException { Date whenDate = parseStr2Date(DatePattern.DATE_TIME_PATTERN, when); return isAfter(date, whenDate); } /** * whether the date is after when * @param date * @param when * @param datePattern * @return * @throws ParseException */ public static boolean isAfter(Date date, String when, DatePattern datePattern) throws ParseException { Date whenDate = parseStr2Date(datePattern, when); return isAfter(date, whenDate); } /** * whether the date is after when * @param date * @param when * @return */ public static boolean isAfter(Date date, Date when) { return date.after(when); } }
目前,自知bug一处: parseStr2Date 方法没去校验传入的dataPattern的格式(此处需用正则,人比较懒,没写)。