package com.chen.util; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * 鏃ユ湡宸ュ叿绫? * */ public final class DateUtils { private DateUtils() { } /** * 杞崲鎸囧畾鏍煎紡鐨勫瓧绗︿覆涓烘椂闂? * * @param str * @param format * @return */ public static Date formatStr2Date(String str, String pattern) { Date d = null; if (null != str && !str.trim().equals("")) { SimpleDateFormat sdf = new SimpleDateFormat(pattern); try { d = sdf.parse(str); } catch (ParseException e) { throw new RuntimeException(e); } } return d; } /** * 杞崲鏃堕棿涓烘寚瀹氭牸寮忕殑瀛楃涓? * * @param date * @return */ public static String formatDate2Str(Date date, String pattern) { if (null == date) { return ""; } SimpleDateFormat sdf = new SimpleDateFormat(pattern); String s = sdf.format(date); return s; } /** * Calendar杞崲涓篋ate * * @param calendar * @return */ public static Date calendar2Date(Calendar calendar) { if (null == calendar) { return null; } return calendar.getTime(); } /** * Date杞崲涓篊alendar * * @param date * @return */ public static Calendar date2Calendar(Date date) { if (null == date) { return null; } Calendar calendar = Calendar.getInstance(); calendar.setTime(date); return calendar; } /** * 璁$畻鎸囧畾鏃ユ湡鏄骞翠腑鐨勭鍑犲懆 * * @param date * @return */ public static Integer getWeekOfYear(Date date) { if (null == date) { return 0; } return date2Calendar(date).get(Calendar.WEEK_OF_YEAR); } /** * 璁$畻鎸囧畾鏃ユ湡鏄骞翠腑鐨勭鍑犲ぉ * * @param date * @return */ public static Integer getDayOfYear(Date date) { if (null == date) { return 0; } return date2Calendar(date).get(Calendar.DAY_OF_YEAR); } /** * 璁$畻鎸囧畾鏃ユ湡鏄鏈堜腑鐨勭鍑犲ぉ * * @param date * @return */ public static Integer getDayOfMonth(Date date) { if (null == date) { return 0; } return date2Calendar(date).get(Calendar.DAY_OF_MONTH); } /** * 璁$畻鎸囧畾鏃ユ湡鏄湀涓殑绗嚑涓槦鏈? * * @param date * @return */ public static Integer getDayOfWeekInMonth(Date date) { if (null == date) { return 0; } return date2Calendar(date).get(Calendar.DAY_OF_WEEK_IN_MONTH); } /** * 璁$畻鎸囧畾鏃ユ湡鏄鍛ㄤ腑鐨勭鍑犲ぉ(鏄熸湡澶?1~~~鏄熸湡鍏?7) * * @param date * @return */ public static Integer getDayOfWeek(Date date) { if (null == date) { return 0; } return date2Calendar(date).get(Calendar.DAY_OF_WEEK); } /** * 璁$畻鎸囧畾鏃ユ湡鏈堜唤鐨勬渶澶уぉ鏁? * * @param date * @return */ public static int getMaxDayInMonth(Date date) { if (date == null) { return 0; } return date2Calendar(date).getActualMaximum(Calendar.DAY_OF_MONTH); } /** * 缁欐寚瀹氭棩鏈熸坊鍔犳垨鍑忓幓鎸囧畾骞? * * @param date * @param amount * @return */ public static Date addYear(Date date, int amount) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.YEAR, amount); return calendar.getTime(); } /** * 鎸囧畾鏃ユ湡鐨勫勾浠? * * @param date * @param year * @return */ public static Date setYear(Date date, int year) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.YEAR, year); return calendar.getTime(); } /** * 缁欐寚瀹氭棩鏈熸坊鍔犳垨鍑忓幓鎸囧畾鏈? * * @param date * @param amount * @return */ public static Date addMonth(Date date, int amount) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.MONTH, amount); return calendar.getTime(); } /** * 鎸囧畾鏃ユ湡鏈堜唤 * * @param date * @param month * @return */ public static Date setMonth(Date date, int month) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.MONTH, month); return calendar.getTime(); } /** * 缁欐寚瀹氭棩鏈熸坊鍔犳垨鍑忓幓鎸囧畾鍛? * * @param date * @param amount * @return */ public static Date addWeek(Date date, int amount) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.WEEK_OF_MONTH, amount); return calendar.getTime(); } /** * 缁欐寚瀹氭棩鏈熸坊鍔犳垨鍑忓幓鎸囧畾澶? * * @param date * @param amount * @return */ public static Date addDay(Date date, int amount) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.DATE, amount); return calendar.getTime(); } /** * 鎸囧畾鏃ユ湡鐨勫ぉ鏁? * * @param date * @param day * @return */ public static Date setDay(Date date, int day) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.DATE, day); return calendar.getTime(); } /** * 缁欐寚瀹氭棩鏈熸坊鍔犳垨鍑忓幓鎸囧畾灏忔椂 * * @param date * @param amount * @return */ public static Date addHour(Date date, int amount) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.HOUR_OF_DAY, amount); return calendar.getTime(); } /** * 鎸囧畾鏃ユ湡鐨勫皬鏃? * * @param date * @param hour * @return */ public static Date setHour(Date date, int hour) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.HOUR_OF_DAY, hour); return calendar.getTime(); } /** * 缁欐寚瀹氭棩鏈熸坊鍔犳垨鍑忓幓鎸囧畾鍒嗛挓 * * @param date * @param amount * @return */ public static Date addMinute(Date date, int amount) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.MINUTE, amount); return calendar.getTime(); } /** * 鎸囧畾鏃ユ湡鐨勫垎閽? * * @param date * @param minute * @return */ public static Date setMinute(Date date, int minute) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.MINUTE, minute); return calendar.getTime(); } /** * 缁欐寚瀹氭棩鏈熸坊鍔犳垨鍑忓幓鎸囧畾绉? * * @param date * @param amount * @return */ public static Date addSecond(Date date, int amount) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.SECOND, amount); return calendar.getTime(); } /** * 鎸囧畾鏃ユ湡鐨勭 * * @param date * @param second * @return */ public static Date setSecond(Date date, int second) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.SECOND, second); return calendar.getTime(); } /** * 缁欐寚瀹氭棩鏈熸坊鍔犳垨鍑忓幓鎸囧畾姣 * * @param date * @param amount * @return */ public static Date addMillisecond(Date date, int amount) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.MILLISECOND, amount); return calendar.getTime(); } /** * 鎸囧畾鏃ユ湡鐨勬绉? * * @param date * @param millisecond * @return */ public static Date setMillisecond(Date date, int millisecond) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.MILLISECOND, millisecond); return calendar.getTime(); } /** * 璁$畻涓や釜鏃ユ湡涔嬮棿宸殑澶氬皯姣锛屽鏋滄棩鏈焒irstDate鍦ㄦ棩鏈焥econdDate鐨勫悗闈㈠垯杩斿洖涓?涓鏁般?佸鏋滄棩鏈焒irstDate鍦ㄦ棩鏈焥econdDate鐨勫墠闈㈠垯杩斿洖涓?涓礋鏁般?佷袱涓棩鏈熺浉鍚岃繑鍥?0 * * @param firstDate * @param secondDate * @return */ public static long compareMillisecond(Date firstDate, Date secondDate) { long time1 = firstDate.getTime(); long time2 = secondDate.getTime(); return (time1 - time2); } /** * 璁$畻涓や釜鏃ユ湡涔嬮棿宸殑澶氬皯绉掞紝濡傛灉鏃ユ湡firstDate鍦ㄦ棩鏈焥econdDate鐨勫悗闈㈠垯杩斿洖涓?涓鏁般?佸鏋滄棩鏈焒irstDate鍦ㄦ棩鏈焥econdDate鐨勫墠闈㈠垯杩斿洖涓?涓礋鏁般?佷袱涓棩鏈熺浉鍚岃繑鍥?0 * * @param firstDate * @param secondDate * @return */ public static long compareSecond(Date firstDate, Date secondDate) { long time1 = firstDate.getTime(); long time2 = secondDate.getTime(); return ((time1 - time2) / (1000)); } /** * 璁$畻涓や釜鏃ユ湡涔嬮棿宸殑澶氬皯鍒嗛挓锛屽鏋滄棩鏈焒irstDate鍦ㄦ棩鏈焥econdDate鐨勫悗闈㈠垯杩斿洖涓?涓鏁般?佸鏋滄棩鏈焒irstDate鍦ㄦ棩鏈焥econdDate鐨勫墠闈㈠垯杩斿洖涓?涓礋鏁般?佷袱涓棩鏈熺浉鍚岃繑鍥?0 * * @param firstDate * @param secondDate * @return */ public static long compareMinute(Date firstDate, Date secondDate) { long time1 = firstDate.getTime(); long time2 = secondDate.getTime(); return ((time1 - time2) / (1000 * 60)); } /** * 璁$畻涓や釜鏃ユ湡涔嬮棿宸殑澶氬皯灏忔椂锛屽鏋滄棩鏈焒irstDate鍦ㄦ棩鏈焥econdDate鐨勫悗闈㈠垯杩斿洖涓?涓鏁般?佸鏋滄棩鏈焒irstDate鍦ㄦ棩鏈焥econdDate鐨勫墠闈㈠垯杩斿洖涓?涓礋鏁般?佷袱涓棩鏈熺浉鍚岃繑鍥?0 * * @param firstDate * @param secondDate * @return */ public static long compareHour(Date firstDate, Date secondDate) { long time1 = firstDate.getTime(); long time2 = secondDate.getTime(); return ((time1 - time2) / (1000 * 60 * 60)); } /** * 璁$畻涓や釜鏃ユ湡涔嬮棿宸殑澶氬皯澶╋紝濡傛灉鏃ユ湡firstDate鍦ㄦ棩鏈焥econdDate鐨勫悗闈㈠垯杩斿洖涓?涓鏁般?佸鏋滄棩鏈焒irstDate鍦ㄦ棩鏈焥econdDate鐨勫墠闈㈠垯杩斿洖涓?涓礋鏁般?佷袱涓棩鏈熺浉鍚岃繑鍥?0 * * @param firstDate * @param secondDate * @return */ public static long compareDay(Date firstDate, Date secondDate) { long time1 = firstDate.getTime(); long time2 = secondDate.getTime(); return ((time1 - time2) / (1000 * 60 * 60 * 24)); } // /** // * // 璁$畻涓や釜鏃ユ湡涔嬮棿宸殑澶氬皯鏈堬紝濡傛灉鏃ユ湡firstDate鍦ㄦ棩鏈焥econdDate鐨勫悗闈㈠垯杩斿洖涓?涓鏁般?佸鏋滄棩鏈焒irstDate鍦ㄦ棩鏈焥econdDate鐨勫墠闈㈠垯杩斿洖涓?涓礋鏁般?佷袱涓棩鏈熺浉鍚岃繑鍥?0 // * // * @param firstDate // * @param secondDate // * @return // */ // public static long compareMonth(Date start, Date end) { // // TODO // if (start.after(end)) { // Date t = start; // start = end; // end = t; // } // Calendar startCalendar = Calendar.getInstance(); // startCalendar.setTime(start); // Calendar endCalendar = Calendar.getInstance(); // endCalendar.setTime(end); // Calendar temp = Calendar.getInstance(); // temp.setTime(end); // temp.add(Calendar.DATE, 1); // // int year = endCalendar.get(Calendar.YEAR) // - startCalendar.get(Calendar.YEAR); // int month = endCalendar.get(Calendar.MONTH) // - startCalendar.get(Calendar.MONTH); // // if ((startCalendar.get(Calendar.DATE) == 1) // && (temp.get(Calendar.DATE) == 1)) { // return year * 12 + month + 1; // } else if ((startCalendar.get(Calendar.DATE) != 1) // && (temp.get(Calendar.DATE) == 1)) { // return year * 12 + month; // } else if ((startCalendar.get(Calendar.DATE) == 1) // && (temp.get(Calendar.DATE) != 1)) { // return year * 12 + month; // } else { // return (year * 12 + month - 1) < 0 ? 0 : (year * 12 + month); // } // } // /** // * // 璁$畻涓や釜鏃ユ湡涔嬮棿宸殑澶氬皯骞达紝濡傛灉鏃ユ湡firstDate鍦ㄦ棩鏈焥econdDate鐨勫悗闈㈠垯杩斿洖涓?涓鏁般?佸鏋滄棩鏈焒irstDate鍦ㄦ棩鏈焥econdDate鐨勫墠闈㈠垯杩斿洖涓?涓礋鏁般?佷袱涓棩鏈熺浉鍚岃繑鍥?0 // * // * @param firstDate // * @param secondDate // * @return // */ // public static long compareYear(Date start, Date end) { // // TODO // return 0; // } /** * 鑾峰彇鎸囧畾鏃ユ湡瀵瑰簲鐨勫勾浠界殑绗竴澶╃殑鏃ユ湡 * * @param date * @return */ public static Date getFirstDayOfYear(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.DAY_OF_YEAR, 1);// 璁句负鏃ユ湡涓轰粖骞寸殑绗?1澶? return calendar.getTime(); } /** * 鑾峰彇鎸囧畾鏃ユ湡瀵瑰簲鐨勫勾浠界殑鏈?鍚庝竴澶╃殑鏃ユ湡 * * @param date * @return */ public static Date getLastDayOfYear(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.DAY_OF_YEAR, 1);// 璁句负鏃ユ湡涓轰粖骞寸殑绗?1澶? calendar.add(Calendar.YEAR, 1);// 鍔犱笂涓?骞达紝鍒扮浜屽勾鐨勭涓?澶? calendar.add(Calendar.DATE, -1);// 鍑忎竴澶╋紝浠婂勾鐨勬渶鍚庝竴澶? return calendar.getTime(); } /** * 鑾峰彇鎸囧畾鏃ユ湡瀵瑰簲鐨勫搴︾殑绗竴澶╃殑鏃ユ湡 * * @param date * @return */ public static Date getFirstDayOfQuarter(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); int month = calendar.get(Calendar.MONTH); if (0 == month || 1 == month || 2 == month) { calendar.set(Calendar.MONDAY, 0); } else if (3 == month || 4 == month || 5 == month) { calendar.set(Calendar.MONDAY, 3); } else if (6 == month || 7 == month || 8 == month) { calendar.set(Calendar.MONDAY, 6); } else { calendar.set(Calendar.MONDAY, 9); } return getFirstDayOfMonth(calendar.getTime()); } /** * 鑾峰彇鎸囧畾鏃ユ湡瀵瑰簲鐨勫搴︾殑鏈?鍚庝竴澶╃殑鏃ユ湡 * * @param date * @return */ public static Date getLastDayOfQuarter(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); int month = calendar.get(Calendar.MONTH); if (0 == month || 1 == month || 2 == month) { calendar.set(Calendar.MONDAY, 2); } else if (3 == month || 4 == month || 5 == month) { calendar.set(Calendar.MONDAY, 5); } else if (6 == month || 7 == month || 8 == month) { calendar.set(Calendar.MONDAY, 8); } else { calendar.set(Calendar.MONDAY, 11); } return getLastDayOfMonth(calendar.getTime()); } /** * 鑾峰彇鎸囧畾鏃ユ湡瀵瑰簲鐨勬湀浠界殑绗竴澶╃殑鏃ユ湡 * * @param date * @return */ public static Date getFirstDayOfMonth(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.DATE, 1);// 璁句负褰撳墠鏈堢殑1鍙? return calendar.getTime(); } /** * 鑾峰彇鎸囧畾鏃ユ湡瀵瑰簲鐨勬湀浠界殑鏈?鍚庝竴澶╃殑鏃ユ湡 * * @param date * @return */ public static Date getLastDayOfMonth(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.DATE, 1);// 璁句负褰撳墠鏈堢殑1鍙? calendar.add(Calendar.MONTH, 1);// 鍔犱竴涓湀锛屽彉涓轰笅鏈堢殑1鍙? calendar.add(Calendar.DATE, -1);// 鍑忓幓涓?澶╋紝鍙樹负褰撴湀鏈?鍚庝竴澶? return calendar.getTime(); } /** * 鑾峰彇鎸囧畾鏃ユ湡瀵瑰簲鐨勫懆鐨勭涓?澶╃殑鏃ユ湡(鎸変腑鍥戒範鎯槦鏈熶竴浣滀负涓?鍛ㄧ殑绗竴澶?) * * @param date * @return */ public static Date getFirstDayOfWeek(Date date) { int dayOfWeek = getDayOfWeek(date); if (2 == dayOfWeek) { return date; } Calendar calendar = Calendar.getInstance(); calendar.setTime(date); while (true) { calendar.add(Calendar.DATE, -1); if (2 == getDayOfWeek(calendar.getTime())) { break; } } return calendar.getTime(); } /** * 鑾峰彇鎸囧畾鏃ユ湡瀵瑰簲鐨勫懆鐨勬渶鍚庝竴澶╃殑鏃ユ湡(鎸変腑鍥戒範鎯槦鏈熷ぉ浣滀负涓?鍛ㄧ殑鏈?鍚庝竴澶?) * * @param date * @return */ public static Date getLastDayOfWeek(Date date) { int dayOfWeek = getDayOfWeek(date); if (1 == dayOfWeek) { return date; } Calendar calendar = Calendar.getInstance(); calendar.setTime(date); while (true) { calendar.add(Calendar.DATE, 1); if (1 == getDayOfWeek(calendar.getTime())) { break; } } return calendar.getTime(); } }

twingo0204 LV24
2016年1月6日
levitation LV7
2012年8月23日

wenpeng182013 LV7
1月6日
13063925092 LV1
2020年5月15日
能不能不存在 LV13
2020年4月21日
fpxrng LV9
2019年12月12日
hzhsh0223 LV18
2019年4月13日
fly666 LV11
2018年9月27日
lw19900921 LV25
2018年9月20日
李杰强 LV18
2018年6月24日
jd_lzh LV1
2018年5月31日
eeeerer LV9
2018年4月25日