package com.qxueyou.scc.base.util; import java.math.BigDecimal; import java.sql.Time; import java.sql.Timestamp; import java.text.DateFormat; import java.text.ParseException; import java.text.ParsePosition; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.Locale; import java.util.TimeZone; import org.apache.commons.lang3.StringUtils; /** * 扩展有关日期工具类的方法。 * * @author 邓志永 * @since JDK1.6 * @history 2014-11-28 邓志永 新建 */ public class DateTimeUtils { /** * 设置 UTC time zone (often referred to as GMT)区域. */ public static final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone("GMT"); /** * YEAR_RANGE_SCOPE */ 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; /** * SEMI_MONTH */ public final static int SEMI_MONTH = 1001; /** * fields */ private static final int[][] FIELDS = { { Calendar.MILLISECOND }, { Calendar.SECOND }, { Calendar.MINUTE }, { Calendar.HOUR_OF_DAY, Calendar.HOUR }, { Calendar.DATE, Calendar.DAY_OF_MONTH, Calendar.AM_PM }, { Calendar.MONTH, DateTimeUtils.SEMI_MONTH }, { Calendar.YEAR }, { Calendar.ERA } }; /** * RANGE_WEEK_SUNDAY */ public final static int RANGE_WEEK_SUNDAY = 1; /** * RANGE_WEEK_MONDAY */ public final static int RANGE_WEEK_MONDAY = 2; /** * RANGE_WEEK_RELATIVE */ public final static int RANGE_WEEK_RELATIVE = 3; /** * RANGE_WEEK_CENTER */ public final static int RANGE_WEEK_CENTER = 4; /** * RANGE_MONTH_SUNDAY */ public final static int RANGE_MONTH_SUNDAY = 5; /** * RANGE_MONTH_MONDAY */ public final static int RANGE_MONTH_MONDAY = 6; /** ISO_DATETIME_FORMAT */ public final static String ISO_DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss"; /** ISO_DATE_FORMAT */ public final static String ISO_DATE_FORMAT = "yyyy-MM-dd"; /** ISO_SHORT_DATE_FORMAT */ public final static String ISO_SHORT_DATE_FORMAT = "yyyy-MM"; /** ISO_DATE_FORMAT */ public final static String ISO_YEAR_FORMAT = "yyyy"; /** ISO_TIME_FORMAT */ public final static String ISO_TIME_FORMAT = "HH:mm:ss"; /** ISO_TIME_FORMAT */ public final static String DATE_IS_NULL = "The date must not be null"; /** * 根据配置的默认日期时间格式来获取指定的对象的字符串信息。 * * @param value * 需要被转换的日期时间对象引用。 * @return 返回的是格式化后的字符串。 */ public static String formatDateTime(Timestamp value) { return formatDateTime(value, ISO_DATETIME_FORMAT); } /** * 根据配置的默认日期时间格式来获取指定的对象的字符串信息。 * * @param value * 需要被转换的日期时间对象引用。 * @param defaultFormat * 按照指定的格式来处理当前的日期时间对象。 * @return 返回的是格式化后的字符串。 */ public static String formatDateTime(Timestamp value, String defaultFormat) { if (value == null) { return ""; } String strFormatStyle = StringUtils.isEmpty(defaultFormat) ? ISO_DATETIME_FORMAT : defaultFormat; SimpleDateFormat objSimpleDateFormat = new SimpleDateFormat(strFormatStyle, Locale.CHINA); return objSimpleDateFormat.format(value); } public static String formatDate(Timestamp value) { return formatDateTime(value, ISO_DATE_FORMAT); } public static String formatYear(Timestamp value) { return formatDateTime(value, ISO_YEAR_FORMAT); } public static String formatDate(Timestamp value, String defaultFormat) { if (value == null) { return ""; } String strFormatStyle = StringUtils.isEmpty(defaultFormat) ? ISO_DATE_FORMAT : defaultFormat; SimpleDateFormat objSimpleDateFormat = new SimpleDateFormat(strFormatStyle, Locale.CHINA); return objSimpleDateFormat.format(value); } public static String formatDate(java.sql.Date value) { return formatDate(value, ISO_DATE_FORMAT); } public static String formatDate(java.util.Date value) { return formatDate(value, ISO_DATE_FORMAT); } public static String formatDate(java.util.Date value, String defaultFormat) { if (value == null) { return ""; } String strFormatStyle = StringUtils.isEmpty(defaultFormat) ? ISO_DATE_FORMAT : defaultFormat; SimpleDateFormat objSimpleDateFormat = new SimpleDateFormat(strFormatStyle, Locale.CHINA); return objSimpleDateFormat.format(value); } public static String formatDate(java.sql.Date value, String defaultFormat) { if (value == null) { return ""; } String strFormatStyle = StringUtils.isEmpty(defaultFormat) ? ISO_DATE_FORMAT : defaultFormat; SimpleDateFormat objSimpleDateFormat = new SimpleDateFormat(strFormatStyle, Locale.CHINA); return objSimpleDateFormat.format(value); } public static String formatTime(Time value, String defaultFormat) { if (value == null) { return ""; } String strFormatStyle = StringUtils.isEmpty(defaultFormat) ? ISO_TIME_FORMAT : defaultFormat; SimpleDateFormat objSimpleDateFormat = new SimpleDateFormat(strFormatStyle, Locale.CHINA); return objSimpleDateFormat.format(value); } public static String formatTime(Time value) { return formatTime(value, ISO_TIME_FORMAT); } public static String formatShortDate(Timestamp value) { return formatDateTime(value, ISO_SHORT_DATE_FORMAT); } public static String formatShortDate(Timestamp value, String defaultFormat) { if (value == null) { return ""; } String strFormatStyle = StringUtils.isEmpty(defaultFormat) ? ISO_SHORT_DATE_FORMAT : defaultFormat; SimpleDateFormat objSimpleDateFormat = new SimpleDateFormat(strFormatStyle, Locale.CHINA); return objSimpleDateFormat.format(value); } /** * 确定两个日期是否是同一天 * * @param dateSource * 需要执行判断的第一个日期时间 * @param dateDesti * 需要执行的第二个日期时间。 * @return true 如果两个日期都代表了同一天,那么将会返回true。 */ public static boolean isSameDay(Date dateSource, Date dateDesti) { if (dateSource == null || dateDesti == null) { throw new IllegalArgumentException(DATE_IS_NULL); } Calendar objCalendarSource = Calendar.getInstance(); objCalendarSource.setTime(dateSource); Calendar objCalendarDesti = Calendar.getInstance(); objCalendarDesti.setTime(dateDesti); return isSameDay(objCalendarSource, objCalendarDesti); } public static boolean isSameDay(Calendar calSource, Calendar calDesti) { if (calSource == null || calDesti == null) { throw new IllegalArgumentException(DATE_IS_NULL); } return calSource.get(Calendar.ERA) == calDesti.get(Calendar.ERA) && calSource.get(Calendar.YEAR) == calDesti.get(Calendar.YEAR) && calSource.get(Calendar.DAY_OF_YEAR) == calDesti.get(Calendar.DAY_OF_YEAR); } public static boolean isSameInstant(Date date1, Date date2) { if (date1 == null || date2 == null) { throw new IllegalArgumentException(DATE_IS_NULL); } return date1.getTime() == date2.getTime(); } public static boolean isSameInstant(Calendar cal1, Calendar cal2) { if (cal1 == null || cal2 == null) { throw new IllegalArgumentException(DATE_IS_NULL); } return cal1.getTime().getTime() == cal2.getTime().getTime(); } public static boolean isSameLocalTime(Calendar calSource, Calendar calDesti) { return calSource.equals(calDesti); } public static Date parseDate(String value, String[] parsePatterns) throws ParseException { if (value == null || parsePatterns == null) { throw new IllegalArgumentException("Date and Patterns must not be null"); } SimpleDateFormat parser = null; ParsePosition pos = new ParsePosition(0); for (int i = 0; i < parsePatterns.length; i++) { if (i == 0) { parser = new SimpleDateFormat(parsePatterns[0],Locale.CHINA); } else { parser.applyPattern(parsePatterns[i]); } pos.setIndex(0); Date date = parser.parse(value, pos); if (date != null && pos.getIndex() == value.length()) { return date; } } throw new ParseException("Unable to parse the date: " + value, -1); } public static java.sql.Date nowAsDate() { Calendar objCurrentCalendar = Calendar.getInstance(); StringBuffer sbDateString = new StringBuffer(); int iYear = objCurrentCalendar.get(Calendar.YEAR); int iMonth = objCurrentCalendar.get(Calendar.MONTH) + 1; int iDate = objCurrentCalendar.get(Calendar.DATE); sbDateString.append(iYear); sbDateString.append('-'); sbDateString.append(iMonth); sbDateString.append('-'); sbDateString.append(iDate); try { return java.sql.Date.valueOf(sbDateString.toString()); } catch (Exception e) { return new java.sql.Date(System.currentTimeMillis()); } } public static Time nowAsTime() { Calendar objCurrentCalendar = Calendar.getInstance(); StringBuffer sbTimeString = new StringBuffer(); int iHourOfDay = objCurrentCalendar.get(Calendar.HOUR_OF_DAY); int iMinute = objCurrentCalendar.get(Calendar.MINUTE); int iSecond = objCurrentCalendar.get(Calendar.SECOND); sbTimeString.append(iHourOfDay); sbTimeString.append(':'); sbTimeString.append(iMinute); sbTimeString.append(':'); sbTimeString.append(iSecond); return java.sql.Time.valueOf(sbTimeString.toString()); } public static Timestamp nowAsTimestamp() { Calendar objCurrentCalendar = Calendar.getInstance(); return new Timestamp(objCurrentCalendar.getTimeInMillis()); } public static Timestamp addYears(Date date, int amount) { return add(date, Calendar.YEAR, amount); } public static Timestamp addMonths(Date date, int amount) { return add(date, Calendar.MONTH, amount); } public static Timestamp addWeeks(Date date, int amount) { return add(date, Calendar.WEEK_OF_YEAR, amount); } public static Timestamp addDays(Date date, int amount) { return add(date, Calendar.DAY_OF_MONTH, amount); } public static Timestamp addHours(Date date, int amount) { return add(date, Calendar.HOUR_OF_DAY, amount); } public static Timestamp addMinutes(Date date, int amount) { return add(date, Calendar.MINUTE, amount); } public static Timestamp addSeconds(Date date, int amount) { return add(date, Calendar.SECOND, amount); } public static Timestamp addMilliseconds(Date date, int amount) { return add(date, Calendar.MILLISECOND, amount); } public static Timestamp add(Date date, int calendarField, int amount) { if (date == null) { throw new IllegalArgumentException(DATE_IS_NULL); } Calendar c = Calendar.getInstance(); c.setTime(date); c.add(calendarField, amount); return new Timestamp(c.getTimeInMillis()); } public static Date round(Date date, int field) { if (date == null) { throw new IllegalArgumentException(DATE_IS_NULL); } Calendar gval = Calendar.getInstance(); gval.setTime(date); modify(gval, field, true); return gval.getTime(); } public static Calendar round(Calendar date, int field) { if (date == null) { throw new IllegalArgumentException(DATE_IS_NULL); } Calendar rounded = (Calendar) date.clone(); modify(rounded, field, true); return rounded; } public static Date round(Object date, int field) { if (date == null) { throw new IllegalArgumentException(DATE_IS_NULL); } if (date instanceof Date) { return round((Date) date, field); } else if (date instanceof Calendar) { return round((Calendar) date, field).getTime(); } else { throw new ClassCastException("Could not round " + date); } } public static Date truncate(Date date, int field) { if (date == null) { throw new IllegalArgumentException(DATE_IS_NULL); } Calendar gval = Calendar.getInstance(); gval.setTime(date); modify(gval, field, false); return gval.getTime(); } public static Calendar truncate(Calendar date, int field) { if (date == null) { throw new IllegalArgumentException(DATE_IS_NULL); } Calendar truncated = (Calendar) date.clone(); modify(truncated, field, false); return truncated; } public static Date truncate(Object date, int field) { if (date == null) { throw new IllegalArgumentException(DATE_IS_NULL); } if (date instanceof Date) { return truncate((Date) date, field); } else if (date instanceof Calendar) { return truncate((Calendar) date, field).getTime(); } else { throw new ClassCastException("Could not truncate " + date); } } private static void modify(Calendar val, int field, boolean round) { if (val.get(Calendar.YEAR) > 280000000) { throw new ArithmeticException("Calendar value too large for accurate calculations"); } if (field == Calendar.MILLISECOND) { return; } Date date = val.getTime(); long time = date.getTime(); boolean done = false; int millisecs = val.get(Calendar.MILLISECOND); if (!round || millisecs < 500) { time = time - millisecs; if (field == Calendar.SECOND) { done = true; } } int seconds = val.get(Calendar.SECOND); if (!done && (!round || seconds < 30)) { time = time - (seconds * 1000L); if (field == Calendar.MINUTE) { done = true; } } int minutes = val.get(Calendar.MINUTE); if (!done && (!round || minutes < 30)) { time = time - (minutes * 60000L); } if (date.getTime() != time) { date.setTime(time); val.setTime(date); } //子方法 modifyChild(val, field, round); throw new IllegalArgumentException("The field " + field + " is not supported"); } private static void modifyChild(Calendar val, int field, boolean round) { boolean roundUp = false; for (int i = 0; i < FIELDS.length; i++) { for (int j = 0; j < FIELDS[i].length; j++) { if (FIELDS[i][j] == field) { if (round && roundUp) { if (field == DateTimeUtils.SEMI_MONTH) { if (val.get(Calendar.DATE) == 1) { val.add(Calendar.DATE, 15); } else { val.add(Calendar.DATE, -15); val.add(Calendar.MONTH, 1); } } else { val.add(FIELDS[i][0], 1); } } return; } } int offset = 0; boolean offsetSet = false; if (field == DateTimeUtils.SEMI_MONTH && FIELDS[i][0] == Calendar.DATE) { offset = val.get(Calendar.DATE) - 1; if (offset >= 15) { offset -= 15; } roundUp = offset > 7; offsetSet = true; } else if (field == Calendar.AM_PM && FIELDS[i][0] == Calendar.HOUR_OF_DAY) { offset = val.get(Calendar.HOUR_OF_DAY); if (offset >= 12) { offset -= 12; } roundUp = offset > 6; offsetSet = true; } if (!offsetSet) { int min = val.getActualMinimum(FIELDS[i][0]); int max = val.getActualMaximum(FIELDS[i][0]); offset = val.get(FIELDS[i][0]) - min; roundUp = offset > ((max - min) / 2); } if (offset != 0) { val.set(FIELDS[i][0], val.get(FIELDS[i][0]) - offset); } } } /** * 得到指定周的第一天(周日)00:00:00秒,返回对应的Timestamp,可以用作时间区间的计算 * * @param cal * Calendar对象,如果为null,则使用本地JVM中的Calendar对象 * @param week * @return */ public static Timestamp getFirstOfWeek(Calendar calen, int week) { Calendar cal = calen; if (cal == null) { cal = Calendar.getInstance(); } cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); return new Timestamp(cal.getTimeInMillis()); } /** * 得到指定周的最后一天(周日)23:23:59秒,返回对应的Timestamp,可以用作时间区间的计算 * * @param cal * Calendar对象,如果为null,则使用本地JVM中的Calendar对象 * @param week * @return */ public static Timestamp getLastOfWeek(Calendar calen, int week) { Calendar cal = calen; if (cal == null) { cal = Calendar.getInstance(); } cal.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY); cal.set(Calendar.HOUR_OF_DAY, 23); cal.set(Calendar.MINUTE, 59); cal.set(Calendar.SECOND, 59); cal.set(Calendar.MILLISECOND, 999); return new Timestamp(cal.getTimeInMillis()); } /** * 得到当前周在一年中的周数 * * @param cal * Calendar对象,如果为null,则使用本地JVM中的Calendar对象 * @return */ public static int getCurrentWeek() { Calendar cal = Calendar.getInstance(); return cal.get(Calendar.WEEK_OF_YEAR); } /** * 得到当前周在一年中的周数 * * @param cal * Calendar对象,如果为null,则使用本地JVM中的Calendar对象 * @return */ public static int getCurrentWeek(Calendar calen) { Calendar cal = calen; if (cal == null) { cal = Calendar.getInstance(); } return cal.get(Calendar.WEEK_OF_YEAR); } public static int getCurrentWeek(Timestamp t1) { Calendar cal; if (t1 == null) { cal = Calendar.getInstance(); } else { cal = new GregorianCalendar(); cal.setTime(t1); } return cal.get(Calendar.WEEK_OF_YEAR); } /** * 得到指定Timestamp代表的日期第一年中第几周 * * @param cal * Calendar对象,如果为null,则使用本地JVM中的Calendar对象 * @param ts * 指定Timestamp * @return 周数 */ public static int getWeekOfYear(Calendar cale, Timestamp ts) { Calendar cal =cale; if (cal == null) { cal = Calendar.getInstance(); } cal.setTimeInMillis(ts.getTime()); return cal.get(Calendar.WEEK_OF_YEAR); } /** * 得到当前年份 * * @return 年 */ public static int getCurrentYear() { Calendar calendar = new GregorianCalendar(); calendar.setTime(new Date()); return calendar.get(Calendar.YEAR); } public static int getYear(Date date) { Calendar calendar = new GregorianCalendar(); calendar.setTime(date); return calendar.get(Calendar.YEAR); } /** * 得到当前月份 * * @return 月份 */ public static int getCurrentMonth() { Calendar calendar = new GregorianCalendar(); calendar.setTime(new Date()); return calendar.get(Calendar.MONTH) + 1; } public static int getCurrentMonth(Timestamp t1) { Calendar calendar = new GregorianCalendar(); calendar.setTime(t1); return calendar.get(Calendar.MONTH) + 1; } public static int getCurrentMonth(Date date) { Calendar calendar = new GregorianCalendar(); calendar.setTime(date); return calendar.get(Calendar.MONTH) + 1; } /** * 获取指定时间的前后多少天的日期以分秒 */ public static Timestamp getTimestamp(Timestamp timestamp, int day, int hour, int minute) { Calendar calendar = new GregorianCalendar(); if (timestamp != null) { calendar.setTimeInMillis(timestamp.getTime()); } else { calendar = Calendar.getInstance(); } calendar.add(Calendar.DATE, day); calendar.set(Calendar.HOUR, hour); calendar.set(Calendar.MINUTE, minute); return new Timestamp(calendar.getTimeInMillis()); } /** * 给出两个日期,计算他们之间相差的年数|月数|天数 * * @param c1 * 日期1 * @param c2 * 日期2 * @param what * 比较模式,如果是Calendar.YEAR则在年份上比较; 如果是Calendar.MONTH则在月数上比较; 如果是Calendar.DATE则在天数上比较(默认情形) * @return 相差的年数或月数或天数 */ public static int compare(Calendar c1, Calendar c2, int what) { int number = 0; switch (what) { case Calendar.YEAR: number = c1.get(Calendar.YEAR) - c2.get(Calendar.YEAR); break; case Calendar.MONTH: int years = compare(c1, c2, Calendar.YEAR); number = 12 * years + (c1.get(Calendar.MONTH) - c2.get(Calendar.MONTH)); break; case Calendar.DATE: number = (int) ((c1.getTimeInMillis() - c2.getTimeInMillis()) / (1000 * 60 * 60 * 24)); break; default: number = (int) ((c1.getTimeInMillis() - c2.getTimeInMillis()) / (1000 * 60 * 60 * 24)); break; } return number; } /* * 得到指定日期的所在年份的最后一天 */ 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);// 日期回滚一天,也就是当年最后一天 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月第一天 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);// 日期回滚一天,也就是最后一天 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);// 把日期设置为当月第一天 return new Timestamp(a.getTimeInMillis()); } /** * 给出两个日期,计算他们之间相差的年数|月数|天数 * * @param c1 * 日期1 * @param c2 * 日期2 * @param what * 比较模式,如果是Calendar.YEAR则在年份上比较; 如果是Calendar.MONTH则在月数上比较; 如果是Calendar.DATE则在天数上比较(默认情形) * @return 相差的年数或月数或天数 */ public static int compare(Timestamp t1, Timestamp t2, int what) { Calendar c1 = Calendar.getInstance(); c1.setTime(t1); Calendar c2 = Calendar.getInstance(); c2.setTime(t2); return compare(c1, c2, what); } public static boolean after(Timestamp t1, Timestamp t2) { if (t1 == null || t2 == null) { return false; } return t1.after(t2); } public static boolean equals(Timestamp t1, Timestamp t2) { if (t1 == null && t2 == null) { return true; } else { if (t1 == null) { return t2.equals(t1); } else { return t1.equals(t2); } } } public static boolean before(Timestamp t1, Timestamp t2) { if (t1 == null || t2 == null) { return false; } return t1.before(t2); } /** * 把字符串的日期转成Timestamp * * @param time * 字符串的日期 * @return int Timestamp型日期 */ public static Timestamp stringConvertTimestamp(String stime) { String time =stime; if (null == time || "".equals(time)) { return null; } if (time.length() == 10) {// yyyy-MM-dd time = time.concat(" 00:00:00.000000000"); } else if (time.length() == 16) {// yyyy-MM-dd hh:mm time = time.concat(":00.000000000"); } else if (time.length() == 19) {// yyyy-MM-dd hh:mm:dd time =time.concat(".000000000"); } return Timestamp.valueOf(time); } /** * 把字符串的日期转成Timestamp * * @param date * 字符串的日期 * @return int Timestamp型日期 */ public static Date stringConvertDate(String sdate) { String date =sdate; if (null == date || "".equals(date)) { return null; } if (date.length() == 10) {// yyyy-MM-dd date = date.concat(" 00:00:00"); } SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.CHINA); Date objDate; try { objDate = formatter.parse(date); } catch (ParseException e) { objDate = null; } return objDate; } /** * 把字符串的日期转成Timestamp * * @param date * 字符串的日期 * @return int Timestamp型日期 */ public static Timestamp stringConvertTS(String sdate) { String date =sdate; if (null == date || "".equals(date)) { return null; } if (date.length() == 10) {// yyyy-MM-dd date = date.concat(" 00:00:00"); } SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.CHINA); Timestamp ts; try { Date objDate = formatter.parse(date); ts = new Timestamp(objDate.getTime()); } catch (ParseException e) { ts = null; } return ts; } /** * 去除date的毫秒数 * * @param date * 字符串的日期 * @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; * @throws ParseException */ public static int compareDate(String date1, String date2, int what) throws ParseException { DateFormat df = new SimpleDateFormat("yyyy-MM-dd",Locale.CHINA); Date objDate1 = df.parse(date1); Date objDate2 = df.parse(date2); return compareDate(objDate1, objDate2, what); } /** * 比较两日期大小 * * @param DATE1 * @param DATE2 * @param what * 比较模式,如果是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(); objCalendar1.setTime(date1); Calendar objCalendar2 = Calendar.getInstance(); objCalendar2.setTime(date2); int iResult = compare(objCalendar1, objCalendar2, what); if (iResult > 0) { return 1; } else if (iResult < 0) { return -1; } else { return 0; } } /** * 和当前日期比较大小 * * @param currentDate * @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; * @throws ParseException */ public static int compareCurrentDate(String currentDate) throws ParseException { DateFormat df = new SimpleDateFormat("yyyy-MM-dd",Locale.CHINA); Date objDate = df.parse(currentDate); return compareDate(objDate, nowAsDate(), Calendar.DATE); } /** * 根据日期,输出日期对应的月份 * * @param date * @return month */ public static int getMonth(Date date) { if (null == date) { throw new IllegalArgumentException("The date is null"); } Calendar calendar = Calendar.getInstance(); calendar.setTime(date); return calendar.get(Calendar.MONTH); } /** * 封装每月的最后一天 * * @param date * @author 伍升存 * @history 2012-03-13 伍升存 * @return */ public static Date lastDayOfMonth(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.set(Calendar.DAY_OF_MONTH, 1); cal.roll(Calendar.DAY_OF_MONTH, -1); return cal.getTime(); } /** * 当前时间的前几个小时 * * @return */ public static Date beforeHourToNowDate(int number) { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - number); return calendar.getTime(); } /** * 当前时间的后几个小时 * * @return */ public static Date afterHourToNowDate(int number) { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) + number); return calendar.getTime(); } // 计算时间 public static String showTime(Date ctime, String sformat) { String format = sformat; String r = ""; if (ctime == null) { return r; } if (format == null) { format = "yyyy-MM-dd HH:mm"; } long nowtimelong = System.currentTimeMillis(); long nowtime = nowtimelong; long ctimelong = ctime.getTime(); long result = nowtime - ctimelong; 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)// 一小时内 { 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)// 一天内 { 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) {// 一个月内 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) {// 一年内 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{// 日期格式 SimpleDateFormat sdf = new SimpleDateFormat(format,Locale.CHINA); r = sdf.format(ctime); } return r; } public static BigDecimal cal(BigDecimal result, long a, int digit) { return result.divide(new BigDecimal(a), digit, BigDecimal.ROUND_HALF_UP); } public static String transForm(Date beginTime, Date endTime) { Calendar cal = Calendar.getInstance(); String trans = ""; 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); int begin_week = cal.get(Calendar.DAY_OF_WEEK); String str_begin_week = getWeek(begin_week); String str_begin_month = str_begin.substring(0, str_begin.indexOf('-')); String str_begin_hour = str_begin.substring(str_begin.indexOf('-') + 1); String str_end_month = str_end.substring(0, str_begin.indexOf('-')); 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; } 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; } return trans; } public static String getWeek(int week) { String str_week = ""; if (week == 2) { str_week = "周一"; } if (week == 3) { str_week = "周二"; } if (week == 4) { str_week = "周三"; } if (week == 5) { str_week = "周四"; } if (week == 6) { str_week = "周五"; } if (week == 7) { str_week = "周六"; } if (week == 1) { str_week = "周日"; } return str_week; } }