| | |
| | | import org.apache.commons.lang3.StringUtils; |
| | | |
| | | /** |
| | | * 扩展有关日期工具类的方法。 |
| | | * 扩展有关日期工具类的方法。 |
| | | * |
| | | * @author 邓志永 |
| | | * @author 邓志永 |
| | | * @since JDK1.6 |
| | | * @history 2014-11-28 邓志永 新建 |
| | | * @history 2014-11-28 邓志永 新建 |
| | | */ |
| | | public class DateTimeUtils { |
| | | |
| | | /** |
| | | * 设置 UTC time zone (often referred to as GMT)区域. |
| | | * 设置 UTC time zone (often referred to as GMT)区域. |
| | | */ |
| | | public static final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone("GMT"); |
| | | |
| | |
| | | public final static int YEAR_RANGE_SCOPE = 4; |
| | | |
| | | /** |
| | | * 设置毫秒和秒之间的对应关系 |
| | | * 设置毫秒和秒之间的对应关系 |
| | | */ |
| | | public static final long MILLIS_PER_SECOND = 1000; |
| | | |
| | | /** |
| | | * 设置毫秒和分之间的关系. |
| | | * 设置毫秒和分之间的关系. |
| | | */ |
| | | public static final long MILLIS_PER_MINUTE = 60 * MILLIS_PER_SECOND; |
| | | |
| | | /** |
| | | * 设置毫秒和分之间的约束关系。 |
| | | * 设置毫秒和分之间的约束关系。 |
| | | */ |
| | | public static final long MILLIS_PER_HOUR = 60 * MILLIS_PER_MINUTE; |
| | | |
| | | /** |
| | | * 设置一天为多少小时. |
| | | * 设置一天为多少小时. |
| | | */ |
| | | public static final long MILLIS_PER_DAY = 24 * MILLIS_PER_HOUR; |
| | | |
| | |
| | | |
| | | |
| | | /** |
| | | * 根据配置的默认日期时间格式来获取指定的对象的字符串信息。 |
| | | * 根据配置的默认日期时间格式来获取指定的对象的字符串信息。 |
| | | * |
| | | * @param value |
| | | * 需要被转换的日期时间对象引用。 |
| | | * @return 返回的是格式化后的字符串。 |
| | | * 需要被转换的日期时间对象引用。 |
| | | * @return 返回的是格式化后的字符串。 |
| | | */ |
| | | public static String formatDateTime(Timestamp value) { |
| | | return formatDateTime(value, ISO_DATETIME_FORMAT); |
| | | } |
| | | |
| | | /** |
| | | * 根据配置的默认日期时间格式来获取指定的对象的字符串信息。 |
| | | * 根据配置的默认日期时间格式来获取指定的对象的字符串信息。 |
| | | * |
| | | * @param value |
| | | * 需要被转换的日期时间对象引用。 |
| | | * 需要被转换的日期时间对象引用。 |
| | | * @param defaultFormat |
| | | * 按照指定的格式来处理当前的日期时间对象。 |
| | | * @return 返回的是格式化后的字符串。 |
| | | * 按照指定的格式来处理当前的日期时间对象。 |
| | | * @return 返回的是格式化后的字符串。 |
| | | */ |
| | | public static String formatDateTime(Timestamp value, String defaultFormat) { |
| | | if (value == null) { |
| | |
| | | } |
| | | |
| | | /** |
| | | * 确定两个日期是否是同一天 |
| | | * 确定两个日期是否是同一天 |
| | | * |
| | | * @param dateSource |
| | | * 需要执行判断的第一个日期时间 |
| | | * 需要执行判断的第一个日期时间 |
| | | * @param dateDesti |
| | | * 需要执行的第二个日期时间。 |
| | | * @return true 如果两个日期都代表了同一天,那么将会返回true。 |
| | | * 需要执行的第二个日期时间。 |
| | | * @return true 如果两个日期都代表了同一天,那么将会返回true。 |
| | | */ |
| | | public static boolean isSameDay(Date dateSource, Date dateDesti) { |
| | | if (dateSource == null || dateDesti == null) { |
| | |
| | | date.setTime(time); |
| | | val.setTime(date); |
| | | } |
| | | //子方法 |
| | | //子方法 |
| | | modifyChild(val, field, round); |
| | | |
| | | throw new IllegalArgumentException("The field " + field + " is not supported"); |
| | |
| | | } |
| | | |
| | | /** |
| | | * 得到指定周的第一天(周日)00:00:00秒,返回对应的Timestamp,可以用作时间区间的计算 |
| | | * 得到指定周的第一天(周日)00:00:00秒,返回对应的Timestamp,可以用作时间区间的计算 |
| | | * |
| | | * @param cal |
| | | * Calendar对象,如果为null,则使用本地JVM中的Calendar对象 |
| | | * Calendar对象,如果为null,则使用本地JVM中的Calendar对象 |
| | | * @param week |
| | | * @return |
| | | */ |
| | |
| | | } |
| | | |
| | | /** |
| | | * 得到指定周的最后一天(周日)23:23:59秒,返回对应的Timestamp,可以用作时间区间的计算 |
| | | * 得到指定周的最后一天(周日)23:23:59秒,返回对应的Timestamp,可以用作时间区间的计算 |
| | | * |
| | | * @param cal |
| | | * Calendar对象,如果为null,则使用本地JVM中的Calendar对象 |
| | | * Calendar对象,如果为null,则使用本地JVM中的Calendar对象 |
| | | * @param week |
| | | * @return |
| | | */ |
| | |
| | | } |
| | | |
| | | /** |
| | | * 得到当前周在一年中的周数 |
| | | * 得到当前周在一年中的周数 |
| | | * |
| | | * @param cal |
| | | * Calendar对象,如果为null,则使用本地JVM中的Calendar对象 |
| | | * Calendar对象,如果为null,则使用本地JVM中的Calendar对象 |
| | | * @return |
| | | */ |
| | | public static int getCurrentWeek() { |
| | |
| | | } |
| | | |
| | | /** |
| | | * 得到当前周在一年中的周数 |
| | | * 得到当前周在一年中的周数 |
| | | * |
| | | * @param cal |
| | | * Calendar对象,如果为null,则使用本地JVM中的Calendar对象 |
| | | * Calendar对象,如果为null,则使用本地JVM中的Calendar对象 |
| | | * @return |
| | | */ |
| | | public static int getCurrentWeek(Calendar calen) { |
| | |
| | | } |
| | | |
| | | /** |
| | | * 得到指定Timestamp代表的日期第一年中第几周 |
| | | * 得到指定Timestamp代表的日期第一年中第几周 |
| | | * |
| | | * @param cal |
| | | * Calendar对象,如果为null,则使用本地JVM中的Calendar对象 |
| | | * Calendar对象,如果为null,则使用本地JVM中的Calendar对象 |
| | | * @param ts |
| | | * 指定Timestamp |
| | | * @return 周数 |
| | | * 指定Timestamp |
| | | * @return 周数 |
| | | */ |
| | | public static int getWeekOfYear(Calendar cale, Timestamp ts) { |
| | | Calendar cal =cale; |
| | |
| | | } |
| | | |
| | | /** |
| | | * 得到当前年份 |
| | | * 得到当前年份 |
| | | * |
| | | * @return 年 |
| | | * @return 年 |
| | | */ |
| | | public static int getCurrentYear() { |
| | | Calendar calendar = new GregorianCalendar(); |
| | |
| | | } |
| | | |
| | | /** |
| | | * 得到当前月份 |
| | | * 得到当前月份 |
| | | * |
| | | * @return 月份 |
| | | * @return 月份 |
| | | */ |
| | | public static int getCurrentMonth() { |
| | | Calendar calendar = new GregorianCalendar(); |
| | |
| | | } |
| | | |
| | | /** |
| | | * 获取指定时间的前后多少天的日期以分秒 |
| | | * 获取指定时间的前后多少天的日期以分秒 |
| | | */ |
| | | public static Timestamp getTimestamp(Timestamp timestamp, int day, int hour, int minute) { |
| | | Calendar calendar = new GregorianCalendar(); |
| | |
| | | } |
| | | |
| | | /** |
| | | * 给出两个日期,计算他们之间相差的年数|月数|天数 |
| | | * 给出两个日期,计算他们之间相差的年数|月数|天数 |
| | | * |
| | | * @param c1 |
| | | * 日期1 |
| | | * 日期1 |
| | | * @param c2 |
| | | * 日期2 |
| | | * 日期2 |
| | | * @param what |
| | | * 比较模式,如果是Calendar.YEAR则在年份上比较; 如果是Calendar.MONTH则在月数上比较; 如果是Calendar.DATE则在天数上比较(默认情形) |
| | | * @return 相差的年数或月数或天数 |
| | | * 比较模式,如果是Calendar.YEAR则在年份上比较; 如果是Calendar.MONTH则在月数上比较; 如果是Calendar.DATE则在天数上比较(默认情形) |
| | | * @return 相差的年数或月数或天数 |
| | | */ |
| | | public static int compare(Calendar c1, Calendar c2, int what) { |
| | | int number = 0; |
| | |
| | | } |
| | | |
| | | /* |
| | | * 得到指定日期的所在年份的最后一天 |
| | | * 得到指定日期的所在年份的最后一天 |
| | | */ |
| | | public static Timestamp getLastOfYear(Timestamp t1) { |
| | | Calendar a = new GregorianCalendar(); |
| | | a.setTime(t1); |
| | | a.set(Calendar.MONTH, 11); |
| | | a.set(Calendar.DATE, 1);// 把日期设置为12月第一天 |
| | | a.roll(Calendar.DATE, -1);// 日期回滚一天,也就是当年最后一天 |
| | | a.set(Calendar.DATE, 1);// 把日期设置为12月第一天 |
| | | a.roll(Calendar.DATE, -1);// 日期回滚一天,也就是当年最后一天 |
| | | return new Timestamp(a.getTimeInMillis()); |
| | | } |
| | | |
| | | /* |
| | | * 得到指定日期的所在年份的第一天 |
| | | * 得到指定日期的所在年份的第一天 |
| | | */ |
| | | public static Timestamp getFirstOfYear(Timestamp t1) { |
| | | Calendar a = new GregorianCalendar(); |
| | | a.setTime(t1); |
| | | a.set(Calendar.MONTH, 0); |
| | | a.set(Calendar.DATE, 1);// 把日期设置为12月第一天 |
| | | a.set(Calendar.DATE, 1);// 把日期设置为12月第一天 |
| | | return new Timestamp(a.getTimeInMillis()); |
| | | } |
| | | |
| | | /* |
| | | * 得到指定日期的所在月份的最后一天 |
| | | * 得到指定日期的所在月份的最后一天 |
| | | */ |
| | | public static Timestamp getLastOfMonth(Timestamp t1) { |
| | | Calendar a = new GregorianCalendar(); |
| | | a.setTime(t1); |
| | | a.set(Calendar.DATE, 1);// 把日期设置为当月第一天 |
| | | a.roll(Calendar.DATE, -1);// 日期回滚一天,也就是最后一天 |
| | | a.set(Calendar.DATE, 1);// 把日期设置为当月第一天 |
| | | a.roll(Calendar.DATE, -1);// 日期回滚一天,也就是最后一天 |
| | | return new Timestamp(a.getTimeInMillis()); |
| | | } |
| | | |
| | | /** |
| | | * 得到指定日期的所在月份的第一天 |
| | | * 得到指定日期的所在月份的第一天 |
| | | * |
| | | * @param t1 |
| | | * @return |
| | |
| | | public static Timestamp getFirstOfMonth(Timestamp t1) { |
| | | Calendar a = new GregorianCalendar(); |
| | | a.setTime(t1); |
| | | a.set(Calendar.DATE, 1);// 把日期设置为当月第一天 |
| | | a.set(Calendar.DATE, 1);// 把日期设置为当月第一天 |
| | | return new Timestamp(a.getTimeInMillis()); |
| | | } |
| | | |
| | | /** |
| | | * 给出两个日期,计算他们之间相差的年数|月数|天数 |
| | | * 给出两个日期,计算他们之间相差的年数|月数|天数 |
| | | * |
| | | * @param c1 |
| | | * 日期1 |
| | | * 日期1 |
| | | * @param c2 |
| | | * 日期2 |
| | | * 日期2 |
| | | * @param what |
| | | * 比较模式,如果是Calendar.YEAR则在年份上比较; 如果是Calendar.MONTH则在月数上比较; 如果是Calendar.DATE则在天数上比较(默认情形) |
| | | * @return 相差的年数或月数或天数 |
| | | * 比较模式,如果是Calendar.YEAR则在年份上比较; 如果是Calendar.MONTH则在月数上比较; 如果是Calendar.DATE则在天数上比较(默认情形) |
| | | * @return 相差的年数或月数或天数 |
| | | */ |
| | | public static int compare(Timestamp t1, Timestamp t2, int what) { |
| | | |
| | |
| | | } |
| | | |
| | | /** |
| | | * 把字符串的日期转成Timestamp |
| | | * 把字符串的日期转成Timestamp |
| | | * |
| | | * @param time |
| | | * 字符串的日期 |
| | | * @return int Timestamp型日期 |
| | | * 字符串的日期 |
| | | * @return int Timestamp型日期 |
| | | */ |
| | | public static Timestamp stringConvertTimestamp(String stime) { |
| | | String time =stime; |
| | |
| | | } |
| | | |
| | | /** |
| | | * 把字符串的日期转成Timestamp |
| | | * 把字符串的日期转成Timestamp |
| | | * |
| | | * @param date |
| | | * 字符串的日期 |
| | | * @return int Timestamp型日期 |
| | | * 字符串的日期 |
| | | * @return int Timestamp型日期 |
| | | */ |
| | | public static Date stringConvertDate(String sdate) { |
| | | String date =sdate; |
| | |
| | | } |
| | | |
| | | /** |
| | | * 把字符串的日期转成Timestamp |
| | | * 把字符串的日期转成Timestamp |
| | | * |
| | | * @param date |
| | | * 字符串的日期 |
| | | * @return int Timestamp型日期 |
| | | * 字符串的日期 |
| | | * @return int Timestamp型日期 |
| | | */ |
| | | public static Timestamp stringConvertTS(String sdate) { |
| | | String date =sdate; |
| | |
| | | |
| | | |
| | | /** |
| | | * 去除date的毫秒数 |
| | | * 去除date的毫秒数 |
| | | * |
| | | * @param date |
| | | * 字符串的日期 |
| | | * @return int Timestamp型日期 |
| | | * 字符串的日期 |
| | | * @return int Timestamp型日期 |
| | | */ |
| | | public static Date getCurrDateTime(Date date) { |
| | | return stringConvertTS(formatDate(date, ISO_DATETIME_FORMAT)); |
| | | } |
| | | |
| | | /** |
| | | * 比较两日期大小 |
| | | * 比较两日期大小 |
| | | * |
| | | * @param DATE1 |
| | | * @param DATE2 |
| | | * @param what |
| | | * 比较模式,如果是Calendar.YEAR则在年份上比较; 如果是Calendar.MONTH则在月数上比较; 如果是Calendar.DATE则在天数上比较(默认情形) |
| | | * @return date1 在date2前=1;date1在date2后=-1;否则=0; |
| | | * 比较模式,如果是Calendar.YEAR则在年份上比较; 如果是Calendar.MONTH则在月数上比较; 如果是Calendar.DATE则在天数上比较(默认情形) |
| | | * @return date1 在date2前=1;date1在date2后=-1;否则=0; |
| | | * @throws ParseException |
| | | */ |
| | | public static int compareDate(String date1, String date2, int what) throws ParseException { |
| | |
| | | } |
| | | |
| | | /** |
| | | * 比较两日期大小 |
| | | * 比较两日期大小 |
| | | * |
| | | * @param DATE1 |
| | | * @param DATE2 |
| | | * @param what |
| | | * 比较模式,如果是Calendar.YEAR则在年份上比较; 如果是Calendar.MONTH则在月数上比较; 如果是Calendar.DATE则在天数上比较(默认情形) |
| | | * @return date1 在date2前=1;date1在date2后=-1;否则=0; |
| | | * 比较模式,如果是Calendar.YEAR则在年份上比较; 如果是Calendar.MONTH则在月数上比较; 如果是Calendar.DATE则在天数上比较(默认情形) |
| | | * @return date1 在date2前=1;date1在date2后=-1;否则=0; |
| | | */ |
| | | public static int compareDate(Date date1, Date date2, int what) { |
| | | Calendar objCalendar1 = Calendar.getInstance(); |
| | |
| | | } |
| | | |
| | | /** |
| | | * 和当前日期比较大小 |
| | | * 和当前日期比较大小 |
| | | * |
| | | * @param currentDate |
| | | * @return currentDate 在当前日期前=1;date1在当前日期后=-1;否则=0; |
| | | * @return currentDate 在当前日期前=1;date1在当前日期后=-1;否则=0; |
| | | */ |
| | | public static int compareCurrentDate(Date currentDate) { |
| | | return compareDate(currentDate, nowAsDate(), Calendar.DATE); |
| | | } |
| | | |
| | | /** |
| | | * 和当前日期比较大小 |
| | | * 和当前日期比较大小 |
| | | * |
| | | * @param currentDate |
| | | * @return currentDate 在当前日期前=1;date1在当前日期后=-1;否则=0; |
| | | * @return currentDate 在当前日期前=1;date1在当前日期后=-1;否则=0; |
| | | * @throws ParseException |
| | | */ |
| | | public static int compareCurrentDate(String currentDate) throws ParseException { |
| | |
| | | } |
| | | |
| | | /** |
| | | * 根据日期,输出日期对应的月份 |
| | | * 根据日期,输出日期对应的月份 |
| | | * |
| | | * @param date |
| | | * @return month |
| | | * @return month |
| | | */ |
| | | public static int getMonth(Date date) { |
| | | if (null == date) { |
| | |
| | | } |
| | | |
| | | /** |
| | | * 封装每月的最后一天 |
| | | * 封装每月的最后一天 |
| | | * |
| | | * @param date |
| | | * @author 伍升存 |
| | | * @history 2012-03-13 伍升存 |
| | | * @author 伍升存 |
| | | * @history 2012-03-13 伍升存 |
| | | * @return |
| | | */ |
| | | public static Date lastDayOfMonth(Date date) { |
| | |
| | | } |
| | | |
| | | /** |
| | | * 当前时间的前几个小时 |
| | | * 当前时间的前几个小时 |
| | | * |
| | | * @return |
| | | */ |
| | |
| | | } |
| | | |
| | | /** |
| | | * 当前时间的后几个小时 |
| | | * 当前时间的后几个小时 |
| | | * |
| | | * @return |
| | | */ |
| | |
| | | return calendar.getTime(); |
| | | } |
| | | |
| | | // 计算时间 |
| | | // 计算时间 |
| | | public static String showTime(Date ctime, String sformat) { |
| | | String format = sformat; |
| | | String r = ""; |
| | |
| | | long nowtime = nowtimelong; |
| | | long ctimelong = ctime.getTime(); |
| | | long result = nowtime - ctimelong; |
| | | if (result - (long) 60 * 1000 < (long) 0)// 一分钟内 |
| | | if (result - (long) 60 * 1000 < (long) 0)// 一分钟内 |
| | | { |
| | | BigDecimal seconds = cal(new BigDecimal(result), (long) 1000, 0); |
| | | r = seconds + "秒钟前"; |
| | | } else if (result - (long) 60 * 1000 >= (long) 0 && result - (long) 60 * 60 * 1000 < (long) 0)// 一小时内 |
| | | r = seconds + "秒钟前"; |
| | | } else if (result - (long) 60 * 1000 >= (long) 0 && result - (long) 60 * 60 * 1000 < (long) 0)// 一小时内 |
| | | { |
| | | BigDecimal seconds = cal(new BigDecimal(result), (long) (60 * 1000), 0); |
| | | r = seconds + "分钟前"; |
| | | } else if (result - (long) 60 * 60 * 1000 >= (long) 0 && result - (long) 24 * 60 * 60 * 1000 < (long) 0)// 一天内 |
| | | r = seconds + "分钟前"; |
| | | } else if (result - (long) 60 * 60 * 1000 >= (long) 0 && result - (long) 24 * 60 * 60 * 1000 < (long) 0)// 一天内 |
| | | { |
| | | BigDecimal seconds = cal(new BigDecimal(result), (long) (60 * 60 * 1000), 0); |
| | | r = seconds + "Сʱǰ"; |
| | | } else if (result - (long) 24 * 60 * 60 * 1000 >= (long) 0 && result - (long) 30 * 24 * 60 * 60 * 1000 < (long) 0) {// 一个月内 |
| | | r = seconds + "小时前"; |
| | | } else if (result - (long) 24 * 60 * 60 * 1000 >= (long) 0 && result - (long) 30 * 24 * 60 * 60 * 1000 < (long) 0) {// 一个月内 |
| | | BigDecimal seconds = cal(new BigDecimal(result), (long) (24 * 60 * 60 * 1000), 0); |
| | | r = seconds + "天前"; |
| | | } else if (result - (long) 30 * 24 * 60 * 60 * 1000 >= (long) 0 && result - (long) 12 * 30 * 24 * 60 * 60 * 1000 < (long) 0) {// 一年内 |
| | | r = seconds + "天前"; |
| | | } else if (result - (long) 30 * 24 * 60 * 60 * 1000 >= (long) 0 && result - (long) 12 * 30 * 24 * 60 * 60 * 1000 < (long) 0) {// 一年内 |
| | | BigDecimal seconds = cal(new BigDecimal(result), new BigDecimal(30).multiply(new BigDecimal(24)).multiply(new BigDecimal(60)).multiply(new BigDecimal(60)).multiply(new BigDecimal(1000)).longValue(), 0); |
| | | r = seconds + "个月前"; |
| | | } else{// 日期格式 |
| | | r = seconds + "个月前"; |
| | | } else{// 日期格式 |
| | | SimpleDateFormat sdf = new SimpleDateFormat(format,Locale.CHINA); |
| | | r = sdf.format(ctime); |
| | | } |
| | |
| | | public static String transForm(Date beginTime, Date endTime) { |
| | | Calendar cal = Calendar.getInstance(); |
| | | String trans = ""; |
| | | SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日-HH:mm",Locale.CHINA); |
| | | SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日-HH:mm",Locale.CHINA); |
| | | String str_begin = sdf.format(beginTime); |
| | | String str_end = sdf.format(endTime); |
| | | cal.setTime(beginTime); |
| | |
| | | String str_end_hour = str_end.substring(str_end.indexOf('-') + 1); |
| | | |
| | | if (str_begin_month.equals(str_end_month)) { |
| | | trans = str_begin_month + " (" + str_begin_week + ") " + str_begin_hour + " 至 " + str_end_hour; |
| | | trans = str_begin_month + " (" + str_begin_week + ") " + str_begin_hour + " 至 " + str_end_hour; |
| | | } else { |
| | | cal.setTime(endTime); |
| | | int end_week = cal.get(Calendar.DAY_OF_WEEK); |
| | | String str_end_week = getWeek(end_week); |
| | | trans = str_begin_month + " (" + str_begin_week + ") " + str_begin_hour + " 至 " + str_end_month + " (" + str_end_week + ") " + str_end_hour; |
| | | trans = str_begin_month + " (" + str_begin_week + ") " + str_begin_hour + " 至 " + str_end_month + " (" + str_end_week + ") " + str_end_hour; |
| | | } |
| | | return trans; |
| | | } |
| | |
| | | public static String getWeek(int week) { |
| | | String str_week = ""; |
| | | if (week == 2) { |
| | | str_week = "周一"; |
| | | str_week = "周一"; |
| | | } |
| | | if (week == 3) { |
| | | str_week = "周二"; |
| | | str_week = "周二"; |
| | | } |
| | | if (week == 4) { |
| | | str_week = "周三"; |
| | | str_week = "周三"; |
| | | } |
| | | if (week == 5) { |
| | | str_week = "周四"; |
| | | str_week = "周四"; |
| | | } |
| | | if (week == 6) { |
| | | str_week = "周五"; |
| | | str_week = "周五"; |
| | | } |
| | | if (week == 7) { |
| | | str_week = "周六"; |
| | | str_week = "周六"; |
| | | } |
| | | if (week == 1) { |
| | | str_week = "周日"; |
| | | str_week = "周日"; |
| | | } |
| | | return str_week; |
| | | |