看到org.apache.commons.httpclient.util包下的DateUtil有點簡單,很多較常用的時間轉換和時間格式轉換都沒有,下面我貼上較為常用的一些關於時間格式以及計算的方法。
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 org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class DateUtil { public static void main(String[] args) { // System.out.println(getStartTime(new Date())); // System.out.println(getEndTime(new Date())); // // // System.out.println("獲取本月第一天日期:" + getFirstDayTimeOfMonth()); // System.out.println("獲取本月最后一天日期:" + getLastDayTimeOfMonth()); // System.out.println("一周前日期:" + date2String(addDays(-7))); // System.out.println("兩天前日期:" + date2String(addDays(-2))); // System.out.println("昨天日期:" + date2String(addDays(-1))); // System.out.println("今天日期:" + date2String(addDays(0))); // System.out.println("明天日期:" + date2String(addDays(1))); // System.out.println("兩天后日期:" + date2String(addDays(2))); // System.out.println("一周后日期:" + date2String(addDays(7))); // // System.out.println("獲取當天日期:" + getNowTime("yyyy-MM-dd")); // System.out.println("獲取本周一日期:" + getMondayOFWeek()); // System.out.println("獲取本周日的日期~:" + getCurrentWeekday()); // System.out.println("獲取上周一日期:" + getPreviousWeekday()); // System.out.println("獲取上周日日期:" + getPreviousWeekSunday()); // System.out.println("獲取下周一日期:" + getNextMonday()); // System.out.println("獲取下周日日期:" + getNextSunday()); // System.out.println("獲取本月第一天日期:" + getFirstDayOfMonth()); // System.out.println("獲取本月最后一天日期:" + getDefaultDay()); // System.out.println("獲取上月第一天日期:" + getPreviousMonthFirst()); // System.out.println("獲取上月最后一天的日期:" + getPreviousMonthEnd()); // System.out.println("獲取下月第一天日期:" + getNextMonthFirst()); // System.out.println("獲取下月最后一天日期:" + getNextMonthEnd()); // System.out.println("獲取本年的第一天日期:" + getCurrentYearFirst()); // System.out.println("獲取本年最后一天日期:" + getCurrentYearEnd()); // System.out.println("獲取去年的第一天日期:" + getPreviousYearFirst()); // System.out.println("獲取去年的最后一天日期:" + getPreviousYearEnd()); // System.out.println("獲取明年第一天日期:" + getNextYearFirst()); // System.out.println("獲取明年最后一天日期:" + getNextYearEnd()); // System.out.println("獲取本季度第一天到最后一天:" + getThisSeasonTime(11)); // System.out.println("獲取兩個日期之間間隔天數2008-12-1~2008-9.29:" + // getTwoDay("2008-12-1", "2008-9-29")); // System.out.println(getNowTimeStampTime().getTime()); System.out.println(getLongAgoYear()); } private static final Logger logger = LoggerFactory.getLogger(DateUtil.class); // 用來全局控制 上一周,本周,下一周的周數變化 private static int weeks = 0; private static int MaxDate;// 一月最大天數 private static int MaxYear;// 一年最大天數 private static String defaultDatePattern = null; private static String timePattern = "HH:mm"; public static final String TS_FORMAT = DateUtil.getDatePattern() + " HH:mm:ss.S"; /** 日期格式yyyy-MM字符串常量 */ public static final String MONTH_FORMAT = "yyyy-MM"; /** 日期格式yyyyMM字符串常量 */ public static final String MONTH_FORMAT_SHORT = "yyyyMM"; /** 日期格式yyyy-MM-dd字符串常量 */ public static final String DATE_FORMAT = "yyyy-MM-dd"; public static final String DATE_FORMAT_DIT = "yyyy.MM.dd"; /** 日期格式yyyyMMdd字符串常量 */ public static final String DATE_FORMAT_SHORT = "yyyyMMdd"; /** 日期格式yyyyMMddHHmm字符串常量 */ public static final String DATE_FORMAT_LONG = "yyyyMMddHHmm"; public static final String DATE_FORMAT_MD = "MMdd"; public static final String DATE_FORMAT_CHN_MD = "M月dd日"; /** 日期格式yyyy MM dd字符串常量 */ public static final String DATE_FORMAT_BANK = "yyyy MM dd"; /** 日期格式HH:mm:ss字符串常量 */ public static final String HOUR_FORMAT = "HH:mm:ss"; /** 日期格式yyyy-MM-dd HH:mm:ss字符串常量 */ public static final String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss"; /** 日期格式yyyy-MM-dd HH:mm:ss字符串常量 */ public static final String DATETIME_FORMAT1 = "yyyy-MM-dd 00:00:00"; /** 日期格式yyyy-MM-dd HH:mm:ss字符串常量 */ public static final String DATETIME_FORMAT0 = "yyyy-MM-dd 01:00:00"; /** 日期格式yyyy-MM-dd HH:mm:ss字符串常量 */ public static final String DATETIME_FORMAT2 = "yyyy-MM-dd 23:59:59"; /** 格式化到小時 */ public static final String HOUR_DATETIME_FORMAT = "yyyy-MM-dd HH"; /** 日期格式yyyy-MM-dd HH:mm:ss字符串常量 */ public static final String MILLISECOND_DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss.S"; /** 日期格式yyyy-MM-dd HH:mm:ss字符串常量 */ public static final String MILLI3SECOND_DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS"; /** 日期格式yyyy-MM-dd HH:mm:ss字符串常量 */ public static final String yyyyMMddHHmmss_FORMAT = "yyyyMMddHHmmss"; public static final String yyyyMMddHHmmssSSS_FORMAT = "yyyyMMddHHmmssSSS"; /** 某天開始時分秒字符串常量 00:00:00 */ public static final String DAY_BEGIN_STRING_HHMMSS = " 00:00:00"; /** 某天結束時分秒字符串常量 23:59:59 */ public static final String DAY_END_STRING_HHMMSS = " 23:59:59"; private static SimpleDateFormat sdf_date_format = new SimpleDateFormat(DATE_FORMAT); private static SimpleDateFormat sdf_date_format_bank = new SimpleDateFormat(DATE_FORMAT_BANK); private static SimpleDateFormat sdf_hour_format = new SimpleDateFormat(HOUR_FORMAT); private static SimpleDateFormat sdf_datetime_format = new SimpleDateFormat(DATETIME_FORMAT); private static SimpleDateFormat sdf_datetime_format2 = new SimpleDateFormat(yyyyMMddHHmmss_FORMAT); public static long calTowDateOfDay(Date endDate,Date nowDate){ long nd = 1000 * 24 * 60 * 60; long diff = endDate.getTime() - nowDate.getTime(); // 計算差多少天 long day = diff / nd; return day; } public static long calTowDateOfHour(Date endDate,Date nowDate){ long nd = 1000 * 24 * 60 * 60; long nh = 1000 * 60 * 60; long diff = endDate.getTime() - nowDate.getTime(); // 計算差多少小時 long hour = diff % nd / nh; return hour; } public static long calTowDateOfMin(Date endDate,Date nowDate){ long nd = 1000 * 24 * 60 * 60; long nh = 1000 * 60 * 60; long nm = 1000 * 60; long diff = endDate.getTime() - nowDate.getTime(); // 計算差多少分鍾 long min = diff % nd % nh / nm; return min; } public static long calTowDateOfSecond(Date endDate,Date nowDate){ long nd = 1000 * 24 * 60 * 60; long nh = 1000 * 60 * 60; long nm = 1000 * 60; long ns = 1000; long diff = endDate.getTime() - nowDate.getTime(); // 計算差多少分鍾 long second = diff % nd % nh % nm / ns; return second; } public static String calTime(Date nowDate,Date create_time){ String subTime=""; long min = 0; long day; long hour = 0; long second = DateUtil.calTowDateOfSecond(nowDate, create_time); min = DateUtil.calTowDateOfMin(nowDate, create_time); hour = DateUtil.calTowDateOfHour(nowDate, create_time); day = DateUtil.calTowDateOfDay(nowDate, create_time); if (day > 0) { subTime = day + "天前"; } else if (hour > 0) { subTime = hour + "小時前"; } else if (min > 0) { subTime = min + "分鍾前"; }else if(second > 0) { subTime = second + "秒前"; } return subTime; } public DateUtil() { } /** * 自定義格式化日期, * @param format * @param date * @return */ public static String formatDateTime(String format,Date date) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format); try { return simpleDateFormat.format(date.getTime()); } catch (Exception e) { logger.debug("DateUtil.getDateTime():" + e.getMessage()); return ""; } } /** * 獲得服務器當前日期及時間,以格式為:yyyy-MM-dd HH:mm:ss的日期字符串形式返回 * * @author dylan_xu * @date Mar 11, 2012 * @return */ public static String getDateTime() { try { return sdf_datetime_format.format(Calendar.getInstance().getTime()); } catch (Exception e) { logger.debug("DateUtil.getDateTime():" + e.getMessage()); return ""; } } /** * 獲得服務器當前日期,以格式為:yyyy-MM-dd的日期字符串形式返回 * * @author dylan_xu * @date Mar 11, 2012 * @return */ public static String getDate() { try { return sdf_date_format.format(Calendar.getInstance().getTime()); } catch (Exception e) { logger.debug("DateUtil.getDate():" + e.getMessage()); return ""; } } /** * 獲得服務器當前日期,以格式為:yyyy MM dd的日期字符串形式返回 * * @author dylan_xu * @date Mar 11, 2012 * @return */ public static String getDateWithBank() { try { return sdf_date_format_bank.format(Calendar.getInstance().getTime()); } catch (Exception e) { logger.debug("DateUtil.getDateWithBank():" + e.getMessage()); return ""; } } /** * 獲得服務器當前時間,以格式為:HH:mm:ss的日期字符串形式返回 * * @author dylan_xu * @date Mar 11, 2012 * @return */ public static String getTime() { String temp = " "; try { temp += sdf_hour_format.format(Calendar.getInstance().getTime()); return temp; } catch (Exception e) { logger.debug("DateUtil.getTime():" + e.getMessage()); return ""; } } /** * 統計時開始日期的默認值 * * @author dylan_xu * @date Mar 11, 2012 * @return */ public static String getStartDate() { try { return getYear() + "-01-01"; } catch (Exception e) { logger.debug("DateUtil.getStartDate():" + e.getMessage()); return ""; } } /** * 統計時結束日期的默認值 * * @author dylan_xu * @date Mar 11, 2012 * @return */ public static String getEndDate() { try { return getDate(); } catch (Exception e) { logger.debug("DateUtil.getEndDate():" + e.getMessage()); return ""; } } /** * 獲得服務器當前日期的年份 * * @author dylan_xu * @date Mar 11, 2012 * @return */ public static String getYear() { try { return String.valueOf(Calendar.getInstance().get(Calendar.YEAR)); } catch (Exception e) { logger.debug("DateUtil.getYear():" + e.getMessage()); return ""; } } /** * 獲得服務器當前日期的年份 * * @author dylan_xu * @date Mar 11, 2012 * @return */ public static Date getLongAgoYear() { Date date = null; try { Calendar calendar = Calendar.getInstance(); calendar.set(1900, 0, 0, 00, 00, 00); date = calendar.getTime(); return date; } catch (Exception e) { logger.debug("DateUtil.getYear():" + e.getMessage()); return null; } } /** * 獲得服務器當前日期的月份 * * @author dylan_xu * @date Mar 11, 2012 * @return */ public static String getMonth() { try { java.text.DecimalFormat df = new java.text.DecimalFormat(); df.applyPattern("00;00"); return df.format((Calendar.getInstance().get(Calendar.MONTH) + 1)); } catch (Exception e) { logger.debug("DateUtil.getMonth():" + e.getMessage()); return ""; } } /** * 獲得服務器在當前月中天數 * * @author dylan_xu * @date Mar 11, 2012 * @return */ public static String getDay() { try { return String.valueOf(Calendar.getInstance().get(Calendar.DAY_OF_MONTH)); } catch (Exception e) { logger.debug("DateUtil.getDay():" + e.getMessage()); return ""; } } /** * 比較兩個日期相差的天數 * * @author dylan_xu * @date Mar 11, 2012 * @param date1 * @param date2 * @return */ public static int getMargin(String date1, String date2) { int margin; try { ParsePosition pos = new ParsePosition(0); ParsePosition pos1 = new ParsePosition(0); Date dt1 = sdf_date_format.parse(date1, pos); Date dt2 = sdf_date_format.parse(date2, pos1); long l = dt1.getTime() - dt2.getTime(); margin = (int) (l / (24 * 60 * 60 * 1000)); return margin; } catch (Exception e) { logger.debug("DateUtil.getMargin():" + e.toString()); return 0; } } /** * 比較兩個日期相差的天數 * * @author dylan_xu * @date Mar 11, 2012 * @param date1 * @param date2 * @return */ public static double getDoubleMargin(String date1, String date2) { double margin; try { ParsePosition pos = new ParsePosition(0); ParsePosition pos1 = new ParsePosition(0); Date dt1 = sdf_datetime_format.parse(date1, pos); Date dt2 = sdf_datetime_format.parse(date2, pos1); long l = dt1.getTime() - dt2.getTime(); margin = (l / (24 * 60 * 60 * 1000.00)); return margin; } catch (Exception e) { logger.debug("DateUtil.getMargin():" + e.toString()); return 0; } } /** * 比較兩個日期相差的月數 * * @author dylan_xu * @date Mar 11, 2012 * @param date1 * @param date2 * @return */ public static int getMonthMargin(String date1, String date2) { int margin; try { margin = (Integer.parseInt(date2.substring(0, 4)) - Integer.parseInt(date1.substring(0, 4))) * 12; margin += (Integer.parseInt(date2.substring(4, 7).replaceAll("-0", "-")) - Integer.parseInt(date1 .substring(4, 7).replaceAll("-0", "-"))); return margin; } catch (Exception e) { logger.debug("DateUtil.getMargin():" + e.toString()); return 0; } } /** * 返回日期加X天后的日期 * * @author dylan_xu * @date Mar 11, 2012 * @param date * @param i * @return */ public static String addDay(String date, int i) { try { GregorianCalendar gCal = new GregorianCalendar(Integer.parseInt(date.substring(0, 4)), Integer.parseInt(date.substring(5, 7)) - 1, Integer.parseInt(date.substring(8, 10))); gCal.add(GregorianCalendar.DATE, i); return sdf_date_format.format(gCal.getTime()); } catch (Exception e) { logger.debug("DateUtil.addDay():" + e.toString()); return getDate(); } } /** * 返回日期加X月后的日期 * * @author dylan_xu * @date Mar 11, 2012 * @param date * @param i * @return */ public static String addMonth(String date, int i) { try { GregorianCalendar gCal = new GregorianCalendar(Integer.parseInt(date.substring(0, 4)), Integer.parseInt(date.substring(5, 7)) - 1, Integer.parseInt(date.substring(8, 10))); gCal.add(GregorianCalendar.MONTH, i); return sdf_date_format.format(gCal.getTime()); } catch (Exception e) { logger.debug("DateUtil.addMonth():" + e.toString()); return getDate(); } } /** * 返回日期加X年后的日期 * * @author dylan_xu * @date Mar 11, 2012 * @param date * @param i * @return */ public static String addYear(String date, int i) { try { GregorianCalendar gCal = new GregorianCalendar(Integer.parseInt(date.substring(0, 4)), Integer.parseInt(date.substring(5, 7)) - 1, Integer.parseInt(date.substring(8, 10))); gCal.add(GregorianCalendar.YEAR, i); return sdf_date_format.format(gCal.getTime()); } catch (Exception e) { logger.debug("DateUtil.addYear():" + e.toString()); return ""; } } /** * 返回某年某月中的最大天 * * @author dylan_xu * @date Mar 11, 2012 * @param year * @param month * @return */ public static int getMaxDay(int iyear, int imonth) { int day = 0; try { if (imonth == 1 || imonth == 3 || imonth == 5 || imonth == 7 || imonth == 8 || imonth == 10 || imonth == 12) { day = 31; } else if (imonth == 4 || imonth == 6 || imonth == 9 || imonth == 11) { day = 30; } else if ((0 == (iyear % 4)) && (0 != (iyear % 100)) || (0 == (iyear % 400))) { day = 29; } else { day = 28; } return day; } catch (Exception e) { logger.debug("DateUtil.getMonthDay():" + e.toString()); return 1; } } /** * 格式化日期 * * @author dylan_xu * @date Mar 11, 2012 * @param orgDate * @param Type * @param Span * @return */ @SuppressWarnings("static-access") public String rollDate(String orgDate, int Type, int Span) { try { String temp = ""; int iyear, imonth, iday; int iPos = 0; char seperater = '-'; if (orgDate == null || orgDate.length() < 6) { return ""; } iPos = orgDate.indexOf(seperater); if (iPos > 0) { iyear = Integer.parseInt(orgDate.substring(0, iPos)); temp = orgDate.substring(iPos + 1); } else { iyear = Integer.parseInt(orgDate.substring(0, 4)); temp = orgDate.substring(4); } iPos = temp.indexOf(seperater); if (iPos > 0) { imonth = Integer.parseInt(temp.substring(0, iPos)); temp = temp.substring(iPos + 1); } else { imonth = Integer.parseInt(temp.substring(0, 2)); temp = temp.substring(2); } imonth--; if (imonth < 0 || imonth > 11) { imonth = 0; } iday = Integer.parseInt(temp); if (iday < 1 || iday > 31) iday = 1; Calendar orgcale = Calendar.getInstance(); orgcale.set(iyear, imonth, iday); temp = this.rollDate(orgcale, Type, Span); return temp; } catch (Exception e) { return ""; } } public static String rollDate(Calendar cal, int Type, int Span) { try { String temp = ""; Calendar rolcale; rolcale = cal; rolcale.add(Type, Span); temp = sdf_date_format.format(rolcale.getTime()); return temp; } catch (Exception e) { return ""; } } /** * 返回默認的日期格式 * * @author dylan_xu * @date Mar 11, 2012 * @return */ public static synchronized String getDatePattern() { defaultDatePattern = DATETIME_FORMAT; return defaultDatePattern; } /** * 將指定日期按默認格式進行格式代化成字符串后輸出如:yyyy-MM-dd * * @author dylan_xu * @date Mar 11, 2012 * @param aDate * @return */ public static final String getDate(Date aDate) { SimpleDateFormat df = null; String returnValue = ""; if (aDate != null) { df = new SimpleDateFormat(getDatePattern()); returnValue = df.format(aDate); } return (returnValue); } /** * 取得給定日期的時間字符串,格式為當前默認時間格式 * * @author dylan_xu * @date Mar 11, 2012 * @param theTime * @return */ public static String getTimeNow(Date theTime) { return getDateTime(timePattern, theTime); } /** * 取得當前時間的Calendar日歷對象 * * @author dylan_xu * @date Mar 11, 2012 * @return * @throws ParseException */ public Calendar getToday() throws ParseException { Date today = new Date(); SimpleDateFormat df = new SimpleDateFormat(getDatePattern()); String todayAsString = df.format(today); Calendar cal = new GregorianCalendar(); cal.setTime(convertStringToDate(todayAsString)); return cal; } /** * 將日期類轉換成指定格式的字符串形式 * * @author dylan_xu * @date Mar 11, 2012 * @param aMask * @param aDate * @return */ public static final String getDateTime(String aMask, Date aDate) { SimpleDateFormat df = null; String returnValue = ""; if (aDate == null) { logger.error("aDate is null!"); } else { df = new SimpleDateFormat(aMask); returnValue = df.format(aDate); } return (returnValue); } /** * 將指定的日期轉換成默認格式的字符串形式 * * @author dylan_xu * @date Mar 11, 2012 * @param aDate * @return */ public static final String convertDateToString(Date aDate) { return getDateTime(getDatePattern(), aDate); } /** * 將日期字符串按指定格式轉換成日期類型 * * @author dylan_xu * @date Mar 11, 2012 * @param aMask * 指定的日期格式,如:yyyy-MM-dd * @param strDate * 待轉換的日期字符串 * @return * @throws ParseException */ public static final Date convertStringToDate(String aMask, String strDate) throws ParseException { SimpleDateFormat df = null; Date date = null; df = new SimpleDateFormat(aMask); if (logger.isDebugEnabled()) { logger.debug("converting '" + strDate + "' to date with mask '" + aMask + "'"); } try { date = df.parse(strDate); } catch (ParseException pe) { logger.error("ParseException: " + pe); throw pe; } return (date); } public static final String formatDate(String format, String strDate) { SimpleDateFormat df = null; Date date = null; df = new SimpleDateFormat(format); if (logger.isDebugEnabled()) { logger.debug("converting ".concat(strDate).concat(" to date with mask ").concat(format)); } try { date = df.parse(strDate); } catch (Exception e) { logger.error(e.getMessage(), e); } return getDateTime(format,date); } /** * 將日期字符串按默認格式轉換成日期類型 * * @author dylan_xu * @date Mar 11, 2012 * @param strDate * @return * @throws ParseException */ public static Date convertStringToDate(String strDate) throws ParseException { Date aDate = null; try { if (logger.isDebugEnabled()) { logger.debug("converting date with pattern: " + getDatePattern()); } aDate = convertStringToDate(getDatePattern(), strDate); } catch (ParseException pe) { logger.error("Could not convert '" + strDate + "' to a date, throwing exception"); throw new ParseException(pe.getMessage(), pe.getErrorOffset()); } return aDate; } /** * 返回一個JAVA簡單類型的日期字符串 * * @author dylan_xu * @date Mar 11, 2012 * @return */ public static String getSimpleDateFormat() { SimpleDateFormat formatter = new SimpleDateFormat(); String NDateTime = formatter.format(new Date()); return NDateTime; } /** * 將指定字符串格式的日期與當前時間比較 * * @author DYLAN * @date Feb 17, 2012 * @param strDate * 需要比較時間 * @return <p> * int code * <ul> * <li>-1 當前時間 < 比較時間</li> * <li>0 當前時間 = 比較時間</li> * <li>>=1當前時間 > 比較時間</li> * </ul> * </p> */ public static int compareToCurTime(String strDate) { if (StringUtils.isBlank(strDate)) { return -1; } Date curTime = Calendar.getInstance().getTime(); String strCurTime = null; try { strCurTime = sdf_datetime_format.format(curTime); } catch (Exception e) { if (logger.isDebugEnabled()) { logger.debug("[Could not format '" + strDate + "' to a date, throwing exception:" + e.getLocalizedMessage() + "]"); } } if (StringUtils.isNotBlank(strCurTime)) { return strCurTime.compareTo(strDate); } return -1; } /** * 為查詢日期添加最小時間 * * @param 目標類型Date * @param 轉換參數Date * @return */ @SuppressWarnings("deprecation") public static Date addStartTime(Date param) { Date date = param; try { date.setHours(0); date.setMinutes(0); date.setSeconds(0); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.MILLISECOND, 0); return calendar.getTime(); } catch (Exception ex) { return date; } } /** * 為查詢日期添加最大時間 * * @param 目標類型Date * @param 轉換參數Date * @return */ @SuppressWarnings("deprecation") public static Date addEndTime(Date param) { Date date = param; try { date.setHours(23); date.setMinutes(59); date.setSeconds(59); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.MILLISECOND, 999); return calendar.getTime(); } catch (Exception ex) { return date; } } /** * 返回系統現在年份中指定月份的天數 * * @param 月份month * @return 指定月的總天數 */ @SuppressWarnings("deprecation") public static String getMonthLastDay(int month) { Date date = new Date(); int[][] day = { { 0, 30, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } }; int year = date.getYear() + 1900; if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { return day[1][month] + ""; } else { return day[0][month] + ""; } } /** * 返回指定年份中指定月份的天數 * * @param 年份year * @param 月份month * @return 指定月的總天數 */ public static String getMonthLastDay(int year, int month) { int[][] day = { { 0, 30, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } }; if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { return day[1][month] + ""; } else { return day[0][month] + ""; } } /** * 判斷是平年還是閏年 * * @author dylan_xu * @date Mar 11, 2012 * @param year * @return */ public static boolean isLeapyear(int year) { if ((year % 4 == 0 && year % 100 != 0) || (year % 400) == 0) { return true; } else { return false; } } /** * 取得當前時間的日戳 * * @author dylan_xu * @date Mar 11, 2012 * @return */ @SuppressWarnings("deprecation") public static String getTimestamp() { Date date = Calendar.getInstance().getTime(); String timestamp = "" + (date.getYear() + 1900) + date.getMonth() + date.getDate() + date.getMinutes() + date.getSeconds() + date.getTime(); return timestamp; } /** * 取得指定時間的日戳 * * @return */ @SuppressWarnings("deprecation") public static String getTimestamp(Date date) { String timestamp = "" + (date.getYear() + 1900) + date.getMonth() + date.getDate() + date.getMinutes() + date.getSeconds() + date.getTime(); return timestamp; } /** * @Title: getStartTime * @Description: 取某一天的最開始的時分秒 * @param param * @return 參數說明 * @return String 返回類型 */ public static String getStartTime(Date param) { return sdf_datetime_format.format(addStartTime(param)); } public static String getStartTime2(Date param) { return sdf_datetime_format2.format(addStartTime(param)); } /** * @Title: getEndTime * @Description: 取某一天的結束時分秒 * @param param * @return 參數說明 * @return String 返回類型 */ public static String getEndTime(Date param) { return sdf_datetime_format.format(addEndTime(param)); } public static String getEndTime2(Date param) { return sdf_datetime_format2.format(addEndTime(param)); } /** * 獲得當前時間,格式yyyy-MM-dd hh:mm:ss * * @param format * @return */ public static String getCurrentDate() { return getCurrentDate(DATETIME_FORMAT); } /** * 獲得當前時間,格式自定義 * * @param format * @return */ public static String getCurrentDate(String format) { if (StringUtils.isBlank(format)) { format = DATETIME_FORMAT; } Calendar day = Calendar.getInstance(); day.add(Calendar.DATE, 0); SimpleDateFormat sdf = new SimpleDateFormat(format);// "yyyy-MM-dd" String date = sdf.format(day.getTime()); return date; } /** * 日期轉字符串 * * @param date * @return */ public static String date2String(Date date) { SimpleDateFormat sdf = new SimpleDateFormat(DATETIME_FORMAT); return sdf.format(date); } /** * 獲得昨天時間 * * @return */ public static String getYesterdayDate() { return getYesterdayDate(DATETIME_FORMAT); } /** * 獲得昨天時間,格式自定義 * * @param format * @return */ public static String getYesterdayDate(String format) { if ("".equals(format)) { format = DATETIME_FORMAT; } Calendar day = Calendar.getInstance(); day.add(Calendar.DATE, -1); SimpleDateFormat sdf = new SimpleDateFormat(format);// "yyyy-MM-dd" String date = sdf.format(day.getTime()); return date; } /** * @param date1 * 需要比較的時間 不能為空(null),需要正確的日期格式 ,如:2009-09-12 * @param date2 * 被比較的時間 為空(null)則為當前時間 * @param stype * 返回值類型 0為多少天,1為多少個月,2為多少年 * @return 舉例: compareDate("2009-09-12", null, 0); //比較天 * compareDate("2009-09-12", null, 1);//比較月 * compareDate("2009-09-12", null, 2);//比較年 */ public static int compareDate(String startDay, String endDay, int stype) { int n = 0; String formatStyle = stype == 1 ? "yyyy-MM" : "yyyy-MM-dd"; endDay = endDay == null ? getCurrentDate("yyyy-MM-dd") : endDay; DateFormat df = new SimpleDateFormat(formatStyle); Calendar c1 = Calendar.getInstance(); Calendar c2 = Calendar.getInstance(); try { c1.setTime(df.parse(startDay)); c2.setTime(df.parse(endDay)); } catch (Exception e3) { System.out.println("wrong occured"); } // List list = new ArrayList(); while (!c1.after(c2)) { // 循環對比,直到相等,n 就是所要的結果 // list.add(df.format(c1.getTime())); // 這里可以把間隔的日期存到數組中 打印出來 n++; if (stype == 1) { c1.add(Calendar.MONTH, 1); // 比較月份,月份+1 } else { c1.add(Calendar.DATE, 1); // 比較天數,日期+1 } } n = n - 1; if (stype == 2) { n = (int) n / 365; } // System.out.println(startDay+" -- "+endDay+" 相差多少"+u[stype]+":"+n); return n; } /** * 判斷時間是否符合時間格式 */ public static boolean isDate(String date, String dateFormat) { if (date != null) { java.text.SimpleDateFormat format = new java.text.SimpleDateFormat(dateFormat); format.setLenient(false); try { format.format(format.parse(date)); } catch (ParseException e) { return false; } return true; } return false; } /** * 實現給定某日期,判斷是星期幾 date:必須yyyy-MM-dd格式 */ public static String getWeekday(String date) { SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat sdw = new SimpleDateFormat("E"); Date d = null; try { d = sd.parse(date); } catch (ParseException e) { logger.error(e.getMessage(), e); } return sdw.format(d); } /** * method 將字符串類型的日期轉換為一個timestamp(時間戳記java.sql.Timestamp) * * @param dateString * 需要轉換為timestamp的字符串 * @return dataTime timestamp */ public final static java.sql.Timestamp string2Time(String dateString) { DateFormat dateFormat; dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);// 設定格式 dateFormat.setLenient(false); java.util.Date date = null; try { date = dateFormat.parse(dateString); } catch (ParseException e) { logger.error(e.getMessage(), e); } // java.sql.Timestamp dateTime = new java.sql.Timestamp(date.getTime()); return new java.sql.Timestamp(date.getTime());// Timestamp類型,timeDate.getTime()返回一個long型 } /** * method 將字符串類型的日期轉換為一個Date(java.sql.Date) * * @param dateString * 需要轉換為Date的字符串 * @return dataTime Date */ public final static java.sql.Date string2Date(String dateString) { DateFormat dateFormat; dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH); dateFormat.setLenient(false); java.util.Date date = null; try { date = dateFormat.parse(dateString); } catch (ParseException e) { logger.error(e.getMessage(), e); } // java.sql.Date dateTime = new java.sql.Date(date.getTime());// sql類型 return new java.sql.Date(date.getTime()); } /** * @Title: string2Date * @Description: * @param dateString * @param df * @return 參數說明 * @return java.util.Date 返回類型 */ public final static java.util.Date string2Date(String dateString,String df) { DateFormat dateFormat = new SimpleDateFormat(df, Locale.SIMPLIFIED_CHINESE); dateFormat.setLenient(false); java.util.Date date = null; try { date = dateFormat.parse(dateString); } catch (ParseException e) { logger.error(e.getMessage(), e); } return date; } /** * 計算兩個日期之間相差的天數 * * @param date1 * @param date2 * @return */ public static int daysBetween(Date date1, Date date2) { Calendar cal = Calendar.getInstance(); cal.setTime(date1); long time1 = cal.getTimeInMillis(); cal.setTime(date2); long time2 = cal.getTimeInMillis(); long between_days = (time2 - time1) / (1000 * 3600 * 24); return Integer.parseInt(String.valueOf(between_days)); } /** * 取得當前日期N天后的日期 * * @param date * @param days * @return */ public static Date addDays(int days) { Calendar cal = Calendar.getInstance(); cal.add(Calendar.DAY_OF_MONTH, days); return cal.getTime(); } /** * 取得指定日期N天后的日期 * * @param date * @param days * @return */ public static Date addXDays(Date date, int days) { Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.add(Calendar.DAY_OF_MONTH, days); return cal.getTime(); } // 記錄考勤, 記錄遲到、早退時間 public static String getState() { String state = "正常"; DateFormat df = new SimpleDateFormat("HH:mm:ss"); Date d = new Date(); try { Date d1 = df.parse("08:00:00"); Date d2 = df.parse(df.format(d)); Date d3 = df.parse("17:30:00"); int t1 = (int) d1.getTime(); int t2 = (int) d2.getTime(); int t3 = (int) d3.getTime(); if (t2 < t1) { long between = (t1 - t2) / 1000;// 除以1000是為了轉換成秒 long hour1 = between % (24 * 3600) / 3600; long minute1 = between % 3600 / 60; state = "遲到 :" + hour1 + "時" + minute1 + "分"; } else if (t2 < t3) { long between = (t3 - t2) / 1000;// 除以1000是為了轉換成秒 long hour1 = between % (24 * 3600) / 3600; long minute1 = between % 3600 / 60; state = "早退 :" + hour1 + "時" + minute1 + "分"; } return state; } catch (Exception e) { return state; } } /** * 得到二個日期間的間隔天數 */ public static String getTwoDay(String sj1, String sj2) { SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd"); long day = 0; try { java.util.Date date = myFormatter.parse(sj1); java.util.Date mydate = myFormatter.parse(sj2); day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000); } catch (Exception e) { return ""; } return day + ""; } /** * 根據一個日期,返回是星期幾的字符串 * * @param sdate * @return */ public static String getWeek(String sdate) { // 再轉換為時間 Date date = strToDate(sdate); Calendar c = Calendar.getInstance(); c.setTime(date); // int hour=c.get(Calendar.DAY_OF_WEEK); // hour中存的就是星期幾了,其范圍 1~7 // 1=星期日 7=星期六,其他類推 return new SimpleDateFormat("EEEE").format(c.getTime()); } /** * 將短時間格式字符串轉換為時間 yyyy-MM-dd * * @param strDate * @return */ public static Date strToDate(String strDate) { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); ParsePosition pos = new ParsePosition(0); Date strtodate = formatter.parse(strDate, pos); return strtodate; } /** * 兩個時間之間的天數 * * @param date1 * @param date2 * @return */ public static long getDays(String date1, String date2) { if (date1 == null || date1.equals("")) return 0; if (date2 == null || date2.equals("")) return 0; // 轉換為標准時間 SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd"); java.util.Date date = null; java.util.Date mydate = null; try { date = myFormatter.parse(date1); mydate = myFormatter.parse(date2); } catch (Exception e) { } long day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000); return day; } // 計算當月最后一天,返回字符串 public static String getDefaultDay() { String str = ""; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Calendar lastDate = Calendar.getInstance(); lastDate.set(Calendar.DATE, 1);// 設為當前月的1號 lastDate.add(Calendar.MONTH, 1);// 加一個月,變為下月的1號 lastDate.add(Calendar.DATE, -1);// 減去一天,變為當月最后一天 str = sdf.format(lastDate.getTime()); return str; } // 計算當月最后一天,返回字符串 public static String getLastDayTimeOfMonth() { String str = ""; SimpleDateFormat sdf = new SimpleDateFormat(DATETIME_FORMAT); Calendar lastDate = Calendar.getInstance(); lastDate.set(Calendar.DATE, 1);// 設為當前月的1號 lastDate.add(Calendar.MONTH, 1);// 加一個月,變為下月的1號 lastDate.add(Calendar.DATE, -1);// 減去一天,變為當月最后一天 lastDate.set(Calendar.HOUR_OF_DAY, 23); lastDate.set(Calendar.MINUTE, 59); lastDate.set(Calendar.SECOND, 59); str = sdf.format(lastDate.getTime()); return str; } // 計算當月最后一天,返回字符串 public static String getLastDayTimeOfMonth2() { String str = ""; SimpleDateFormat sdf = new SimpleDateFormat(yyyyMMddHHmmss_FORMAT); Calendar lastDate = Calendar.getInstance(); lastDate.set(Calendar.DATE, 1);// 設為當前月的1號 lastDate.add(Calendar.MONTH, 1);// 加一個月,變為下月的1號 lastDate.add(Calendar.DATE, -1);// 減去一天,變為當月最后一天 lastDate.set(Calendar.HOUR_OF_DAY, 23); lastDate.set(Calendar.MINUTE, 59); lastDate.set(Calendar.SECOND, 59); str = sdf.format(lastDate.getTime()); return str; } // 上月第一天 public static String getPreviousMonthFirst() { String str = ""; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Calendar lastDate = Calendar.getInstance(); lastDate.set(Calendar.DATE, 1);// 設為當前月的1號 lastDate.add(Calendar.MONTH, -1);// 減一個月,變為下月的1號 // lastDate.add(Calendar.DATE,-1);//減去一天,變為當月最后一天 str = sdf.format(lastDate.getTime()); return str; } // 獲取當月第一天 public static String getFirstDayOfMonth() { String str = ""; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Calendar lastDate = Calendar.getInstance(); lastDate.set(Calendar.DATE, 1);// 設為當前月的1號 str = sdf.format(lastDate.getTime()); return str; } public static String getFirstDayTimeOfMonth() { String str = ""; SimpleDateFormat sdf = new SimpleDateFormat(DATETIME_FORMAT); Calendar lastDate = Calendar.getInstance(); lastDate.set(Calendar.DATE, 1);// 設為當前月的1號 lastDate.set(Calendar.HOUR_OF_DAY, 0); lastDate.set(Calendar.MINUTE, 0); lastDate.set(Calendar.SECOND, 0); str = sdf.format(lastDate.getTime()); return str; } public static String getFirstDayTimeOfMonth2() { String str = ""; SimpleDateFormat sdf = new SimpleDateFormat(yyyyMMddHHmmss_FORMAT); Calendar lastDate = Calendar.getInstance(); lastDate.set(Calendar.DATE, 1);// 設為當前月的1號 lastDate.set(Calendar.HOUR_OF_DAY, 0); lastDate.set(Calendar.MINUTE, 0); lastDate.set(Calendar.SECOND, 0); str = sdf.format(lastDate.getTime()); return str; } // 獲得本周星期日的日期 public static String getCurrentWeekday() { weeks = 0; int mondayPlus = getMondayPlus(); GregorianCalendar currentDate = new GregorianCalendar(); currentDate.add(GregorianCalendar.DATE, mondayPlus + 6); Date monday = currentDate.getTime(); DateFormat df = DateFormat.getDateInstance(); String preMonday = df.format(monday); return preMonday; } // 獲取當天時間 public static String getNowTime(String dateformat) { Date now = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat(dateformat);// 可以方便地修改日期格式 String hehe = dateFormat.format(now); return hehe; } // 獲得當前日期與本周日相差的天數 public static int getMondayPlus() { Calendar cd = Calendar.getInstance(); // 獲得今天是一周的第幾天,星期日是第一天,星期二是第二天...... int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK) - 1; // 因為按中國禮拜一作為第一天所以這里減1 if (dayOfWeek == 1) { return 0; } else { return 1 - dayOfWeek; } } // 獲得本周一的日期 public static String getMondayOFWeek() { weeks = 0; int mondayPlus = getMondayPlus(); GregorianCalendar currentDate = new GregorianCalendar(); currentDate.add(GregorianCalendar.DATE, mondayPlus); Date monday = currentDate.getTime(); DateFormat df = DateFormat.getDateInstance(); String preMonday = df.format(monday); return preMonday; } // 獲得相應周的周六的日期 public static String getSaturday() { int mondayPlus = getMondayPlus(); GregorianCalendar currentDate = new GregorianCalendar(); currentDate.add(GregorianCalendar.DATE, mondayPlus + 7 * weeks + 6); Date monday = currentDate.getTime(); DateFormat df = DateFormat.getDateInstance(); String preMonday = df.format(monday); return preMonday; } // 獲得上周星期日的日期 public static String getPreviousWeekSunday() { weeks = 0; weeks--; int mondayPlus = getMondayPlus(); GregorianCalendar currentDate = new GregorianCalendar(); currentDate.add(GregorianCalendar.DATE, mondayPlus + weeks); Date monday = currentDate.getTime(); DateFormat df = DateFormat.getDateInstance(); String preMonday = df.format(monday); return preMonday; } // 獲得上周星期一的日期 public static String getPreviousWeekday() { weeks--; int mondayPlus = getMondayPlus(); GregorianCalendar currentDate = new GregorianCalendar(); currentDate.add(GregorianCalendar.DATE, mondayPlus + 7 * weeks); Date monday = currentDate.getTime(); DateFormat df = DateFormat.getDateInstance(); String preMonday = df.format(monday); return preMonday; } // 獲得下周星期一的日期 public static String getNextMonday() { weeks++; int mondayPlus = getMondayPlus(); GregorianCalendar currentDate = new GregorianCalendar(); currentDate.add(GregorianCalendar.DATE, mondayPlus + 7); Date monday = currentDate.getTime(); DateFormat df = DateFormat.getDateInstance(); String preMonday = df.format(monday); return preMonday; } // 獲得下周星期日的日期 public static String getNextSunday() { int mondayPlus = getMondayPlus(); GregorianCalendar currentDate = new GregorianCalendar(); currentDate.add(GregorianCalendar.DATE, mondayPlus + 7 + 6); Date monday = currentDate.getTime(); DateFormat df = DateFormat.getDateInstance(); String preMonday = df.format(monday); return preMonday; } public static int getMonthPlus() { Calendar cd = Calendar.getInstance(); int monthOfNumber = cd.get(Calendar.DAY_OF_MONTH); cd.set(Calendar.DATE, 1);// 把日期設置為當月第一天 cd.roll(Calendar.DATE, -1);// 日期回滾一天,也就是最后一天 MaxDate = cd.get(Calendar.DATE); if (monthOfNumber == 1) { return -MaxDate; } else { return 1 - monthOfNumber; } } // 獲得上月最后一天的日期 public static String getPreviousMonthEnd() { String str = ""; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Calendar lastDate = Calendar.getInstance(); lastDate.add(Calendar.MONTH, -1);// 減一個月 lastDate.set(Calendar.DATE, 1);// 把日期設置為當月第一天 lastDate.roll(Calendar.DATE, -1);// 日期回滾一天,也就是本月最后一天 str = sdf.format(lastDate.getTime()); return str; } // 獲得下個月第一天的日期 public static String getNextMonthFirst() { String str = ""; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Calendar lastDate = Calendar.getInstance(); lastDate.add(Calendar.MONTH, 1);// 減一個月 lastDate.set(Calendar.DATE, 1);// 把日期設置為當月第一天 str = sdf.format(lastDate.getTime()); return str; } // 獲得下個月最后一天的日期 public static String getNextMonthEnd() { String str = ""; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Calendar lastDate = Calendar.getInstance(); lastDate.add(Calendar.MONTH, 1);// 加一個月 lastDate.set(Calendar.DATE, 1);// 把日期設置為當月第一天 lastDate.roll(Calendar.DATE, -1);// 日期回滾一天,也就是本月最后一天 str = sdf.format(lastDate.getTime()); return str; } // 獲得明年最后一天的日期 public static String getNextYearEnd() { String str = ""; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Calendar lastDate = Calendar.getInstance(); lastDate.add(Calendar.YEAR, 1);// 加一個年 lastDate.set(Calendar.DAY_OF_YEAR, 1); lastDate.roll(Calendar.DAY_OF_YEAR, -1); str = sdf.format(lastDate.getTime()); return str; } // 獲得明年第一天的日期 public static String getNextYearFirst() { String str = ""; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Calendar lastDate = Calendar.getInstance(); lastDate.add(Calendar.YEAR, 1);// 加一個年 lastDate.set(Calendar.DAY_OF_YEAR, 1); str = sdf.format(lastDate.getTime()); return str; } // 獲得本年有多少天 public static int getMaxYear() { Calendar cd = Calendar.getInstance(); cd.set(Calendar.DAY_OF_YEAR, 1);// 把日期設為當年第一天 cd.roll(Calendar.DAY_OF_YEAR, -1);// 把日期回滾一天。 int MaxYear = cd.get(Calendar.DAY_OF_YEAR); return MaxYear; } public static int getYearPlus() { Calendar cd = Calendar.getInstance(); int yearOfNumber = cd.get(Calendar.DAY_OF_YEAR);// 獲得當天是一年中的第幾天 cd.set(Calendar.DAY_OF_YEAR, 1);// 把日期設為當年第一天 cd.roll(Calendar.DAY_OF_YEAR, -1);// 把日期回滾一天。 int MaxYear = cd.get(Calendar.DAY_OF_YEAR); if (yearOfNumber == 1) { return -MaxYear; } else { return 1 - yearOfNumber; } } // 獲得本年第一天的日期 public static String getCurrentYearFirst() { int yearPlus = getYearPlus(); GregorianCalendar currentDate = new GregorianCalendar(); currentDate.add(GregorianCalendar.DATE, yearPlus); Date yearDay = currentDate.getTime(); DateFormat df = DateFormat.getDateInstance(); String preYearDay = df.format(yearDay); return preYearDay; } // 獲得本年最后一天的日期 * public static String getCurrentYearEnd() { Date date = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");// 可以方便地修改日期格式 String years = dateFormat.format(date); return years + "-12-31"; } // 獲得上年第一天的日期 * public static String getPreviousYearFirst() { Date date = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");// 可以方便地修改日期格式 String years = dateFormat.format(date); int years_value = Integer.parseInt(years); years_value--; return years_value + "-1-1"; } // 獲得上年最后一天的日期 public static String getPreviousYearEnd() { weeks--; int yearPlus = getYearPlus(); GregorianCalendar currentDate = new GregorianCalendar(); currentDate.add(GregorianCalendar.DATE, yearPlus + MaxYear * weeks + (MaxYear - 1)); Date yearDay = currentDate.getTime(); DateFormat df = DateFormat.getDateInstance(); String preYearDay = df.format(yearDay); getThisSeasonTime(11); return preYearDay; } // 獲得本季度 public static String getThisSeasonTime(int month) { int array[][] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }, { 10, 11, 12 } }; int season = 1; if (month >= 1 && month <= 3) { season = 1; } if (month >= 4 && month <= 6) { season = 2; } if (month >= 7 && month <= 9) { season = 3; } if (month >= 10 && month <= 12) { season = 4; } int start_month = array[season - 1][0]; int end_month = array[season - 1][2]; Date date = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");// 可以方便地修改日期格式 String years = dateFormat.format(date); int years_value = Integer.parseInt(years); int start_days = 1;// years+"-"+String.valueOf(start_month)+"-1";//getLastDayOfMonth(years_value,start_month); int end_days = getLastDayOfMonth(years_value, end_month); String seasonDate = years_value + "-" + start_month + "-" + start_days + ";" + years_value + "-" + end_month + "-" + end_days; return seasonDate; } /** * 獲取某年某月的最后一天 * * @param year * 年 * @param month * 月 * @return 最后一天 */ public static int getLastDayOfMonth(int year, int month) { if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) { return 31; } if (month == 4 || month == 6 || month == 9 || month == 11) { return 30; } if (month == 2) { if (isLeapYear(year)) { return 29; } else { return 28; } } return 0; } /** * 是否閏年 * * @param year * 年 * @return */ public static boolean isLeapYear(int year) { return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); } /** * * @Title: getNowTimeStampTime * @Description: 獲取當前時間(TimeStamp類型的) * @return * @return Timestamp * @throws */ public static Timestamp getNowTimeStampTime() { Timestamp time = new Timestamp(System.currentTimeMillis()); return time; } /** * * @Title: calcTwoTimeDifferInOneHour * @Description: 計算最后一次訪問時間距離現在時間是不是在1小時之內。 * @param lastestVisitTime * 最后一次訪問的毫秒時間 * @param nowTime * 現在的毫秒時間 * @return void * @throws */ public static boolean calcTwoTimeDifferInOneHour(Long lastestVisitTime, Long nowTime) { long differ = nowTime - lastestVisitTime;// 兩個時間的毫秒差 long differHour = differ / (60 * 60 * 1000); if (differHour < 1) { return true; } return false; } /** * @Title: compareDate * @Description: 比較時間大小(第一個時間在第二個時間之前返回-1;第一個時間在第二個時間之后返回1,相同返回0) * @param dt1 * @param dt2 * @return 參數說明 * @return int 返回類型 */ public static int compareDate(Date dt1, Date dt2) { try { if (dt1.getTime() > dt2.getTime()) { return 1; } else if (dt1.getTime() < dt2.getTime()) { return -1; } else { return 0; } } catch (Exception e) { logger.error(e.getMessage(), e); } return 0; } public static String getPreviousMonth(String dateFormat) { String str = ""; SimpleDateFormat sdf = new SimpleDateFormat(dateFormat); Calendar lastDate = Calendar.getInstance(); lastDate.add(Calendar.MONTH, -1); str = sdf.format(lastDate.getTime()); return str; } }