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; } }