java常用日期操作方法
阿新 • • 發佈:2019-01-03
package com.wujiangpo.test.util; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.regex.Pattern; public class DateUtil { private static final int[] DAY_OF_MONTH = new int[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; /** * * @param strDate * 輸入日期 * @param dayNum * 相隔天數 正整數表示前推 ,負整數表示後推 * @return 日期字串 * @throws ParseException */ public static String getDifferDate(String strDate, int dayNum) throws ParseException { SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd"); Date date = sf.parse(strDate); Calendar c = Calendar.getInstance(); c.setTime(date); c.add(Calendar.DATE, dayNum); String sqldate = sf.format(c.getTime()); return sqldate; } /** * 取得當前日期相隔dayNum天的日期 * * @param strDate * @param dayNum * @return * @throws ParseException */ public static String getDifferDate(int dayNum) throws ParseException { Date date = new Date(); SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd"); Calendar c = Calendar.getInstance(); c.setTime(date); c.add(Calendar.DATE, dayNum); String strDate = sf.format(c.getTime()); return strDate; } /** * 把日期字串轉換為util.date型別 * * @param strDate * 日期字串(yyyy-MM-dd) * * * @return java.sql.date 型別 */ public static java.util.Date getDate(String strDate) { return java.sql.Date.valueOf(strDate); } /** * 校驗日期YYYY-MM-DD格式是否正確 * * @param date * @return */ public static boolean checkDateForm(String date) { if (date == null || "".equals(date)) return false; String eL = "^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))"; return Pattern.compile(eL).matcher(date).matches(); } /** * 取得指定天數後的時間 * * @param date * 基準時間 * @param dayAmount * 指定天數,允許為負數 * @return 指定天數後的時間 */ public static Date addDay(Date date, int dayAmount) { if (date == null) { return null; } Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.DATE, dayAmount); return calendar.getTime(); } /** * 取得指定小時數後的時間 * * @param date * 基準時間 * @param hourAmount * 指定小時數,允許為負數 * @return 指定小時數後的時間 */ public static Date addHour(Date date, int hourAmount) { if (date == null) { return null; } Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.HOUR, hourAmount); return calendar.getTime(); } /** * 取得指定分鐘數後的時間 * * @param date * 基準時間 * @param minuteAmount * 指定分鐘數,允許為負數 * @return 指定分鐘數後的時間 */ public static Date addMinute(Date date, int minuteAmount) { if (date == null) { return null; } Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.MINUTE, minuteAmount); return calendar.getTime(); } /** * 比較兩日期物件中的小時和分鐘部分的大小. * * @param date * 日期物件1, 如果為 <code>null</code> 會以當前時間的日期物件代替 * @param anotherDate * 日期物件2, 如果為 <code>null</code> 會以當前時間的日期物件代替 * @return 如果日期物件1大於日期物件2, 則返回大於0的數; 反之返回小於0的數; 如果兩日期物件相等, 則返回0. */ public static int compareHourAndMinute(Date date, Date anotherDate) { if (date == null) { date = new Date(); } if (anotherDate == null) { anotherDate = new Date(); } Calendar cal = Calendar.getInstance(); cal.setTime(date); int hourOfDay1 = cal.get(Calendar.HOUR_OF_DAY); int minute1 = cal.get(Calendar.MINUTE); cal.setTime(anotherDate); int hourOfDay2 = cal.get(Calendar.HOUR_OF_DAY); int minute2 = cal.get(Calendar.MINUTE); if (hourOfDay1 > hourOfDay2) { return 1; } else if (hourOfDay1 == hourOfDay2) { // 小時相等就比較分鐘 return minute1 > minute2 ? 1 : (minute1 == minute2 ? 0 : -1); } else { return -1; } } /** * 比較兩日期物件的大小, 忽略秒, 只精確到分鐘. * * @param date * 日期物件1, 如果為 <code>null</code> 會以當前時間的日期物件代替 * @param anotherDate * 日期物件2, 如果為 <code>null</code> 會以當前時間的日期物件代替 * @return 如果日期物件1大於日期物件2, 則返回大於0的數; 反之返回小於0的數; 如果兩日期物件相等, 則返回0. */ public static int compareIgnoreSecond(Date date, Date anotherDate) { if (date == null) { date = new Date(); } if (anotherDate == null) { anotherDate = new Date(); } Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); date = cal.getTime(); cal.setTime(anotherDate); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); anotherDate = cal.getTime(); return date.compareTo(anotherDate); } /** * 取得當前時間的字串表示,格式為2006-01-10 20:56:30.756 * * @return 當前時間的字串表示 */ public static String currentDate2String() { return date2String(new Date()); } /** * 取得當前時間的字串表示,格式為2006-01-10 * * @return 當前時間的字串表示 */ public static String currentDate2StringByDay() { return date2StringByDay(new Date()); } /** * 取得今天的最後一個時刻 * * @return 今天的最後一個時刻 */ public static Date currentEndDate() { return getEndDate(new Date()); } /** * 取得今天的第一個時刻 * * @return 今天的第一個時刻 */ public static Date currentStartDate() { return getStartDate(new Date()); } /** * 把時間轉換成字串,格式為2006-01-10 20:56:30.756 * * @param date * 時間 * @return 時間字串 */ public static String date2String(Date date) { return date2String(date, "yyyy-MM-dd HH:mm:ss.SSS"); } /** * 按照指定格式把時間轉換成字串,格式的寫法類似yyyy-MM-dd HH:mm:ss.SSS * * @param date * 時間 * @param pattern * 格式 * @return 時間字串 */ public static String date2String(Date date, String pattern) { if (date == null) { return null; } return (new SimpleDateFormat(pattern)).format(date); } /** * 把時間轉換成字串,格式為2006-01-10 * * @param date * 時間 * @return 時間字串 */ public static String date2StringByDay(Date date) { return date2String(date, "yyyy-MM-dd"); } /** * 把時間轉換成字串,格式為2006-01-10 20:56 * * @param date * 時間 * @return 時間字串 */ public static String date2StringByMinute(Date date) { return date2String(date, "yyyy-MM-dd HH:mm"); } /** * 把時間轉換成字串,格式為2006-01-10 20:56:30 * * @param date * 時間 * @return 時間字串 */ public static String date2StringBySecond(Date date) { return date2String(date, "yyyy-MM-dd HH:mm:ss"); } /** * 根據某星期幾的英文名稱來獲取該星期幾的中文數. <br> * e.g. <li>monday -> 一</li> <li>sunday -> 日</li> * * @param englishWeekName * 星期的英文名稱 * @return 星期的中文數 */ public static String getChineseWeekNumber(String englishWeekName) { if ("monday".equalsIgnoreCase(englishWeekName)) { return "一"; } if ("tuesday".equalsIgnoreCase(englishWeekName)) { return "二"; } if ("wednesday".equalsIgnoreCase(englishWeekName)) { return "三"; } if ("thursday".equalsIgnoreCase(englishWeekName)) { return "四"; } if ("friday".equalsIgnoreCase(englishWeekName)) { return "五"; } if ("saturday".equalsIgnoreCase(englishWeekName)) { return "六"; } if ("sunday".equalsIgnoreCase(englishWeekName)) { return "日"; } return null; } /** * 根據指定的年, 月, 日等引數獲取日期物件. * * @param year * 年 * @param month * 月 * @param date * 日 * @return 對應的日期物件 */ public static Date getDate(int year, int month, int date) { return getDate(year, month, date, 0, 0); } /** * 根據指定的年, 月, 日, 時, 分等引數獲取日期物件. * * @param year * 年 * @param month * 月 * @param date * 日 * @param hourOfDay * 時(24小時制) * @param minute * 分 * @return 對應的日期物件 */ public static Date getDate(int year, int month, int date, int hourOfDay, int minute) { return getDate(year, month, date, hourOfDay, minute, 0); } /** * 根據指定的年, 月, 日, 時, 分, 秒等引數獲取日期物件. * * @param year * 年 * @param month * 月 * @param date * 日 * @param hourOfDay * 時(24小時制) * @param minute * 分 * @param second * 秒 * @return 對應的日期物件 */ public static Date getDate(int year, int month, int date, int hourOfDay, int minute, int second) { Calendar cal = Calendar.getInstance(); cal.set(year, month - 1, date, hourOfDay, minute, second); cal.set(Calendar.MILLISECOND, 0); return cal.getTime(); } /** * 取得某個日期是星期幾,星期日是1,依此類推 * * @param date * 日期 * @return 星期幾 */ public static int getDayOfWeek(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); return cal.get(Calendar.DAY_OF_WEEK); } /** * 獲取某天的結束時間, e.g. 2005-10-01 23:59:59.999 * * @param date * 日期物件 * @return 該天的結束時間 */ public static Date getEndDate(Date date) { if (date == null) { return null; } Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.set(Calendar.HOUR_OF_DAY, 23); cal.set(Calendar.MINUTE, 59); cal.set(Calendar.SECOND, 59); cal.set(Calendar.MILLISECOND, 999); return cal.getTime(); } /** * 取得一個月最多的天數 * * @param year * 年份 * @param month * 月份,0表示1月,依此類推 * @return 最多的天數 */ public static int getMaxDayOfMonth(int year, int month) { if (month == 1 && isLeapYear(year)) { return 29; } return DAY_OF_MONTH[month]; } /** * 得到指定日期的下一天 * * @param date * 日期物件 * @return 同一時間的下一天的日期物件 */ public static Date getNextDay(Date date) { return addDay(date, 1); } /** * 獲取某天的起始時間, e.g. 2005-10-01 00:00:00.000 * * @param date * 日期物件 * @return 該天的起始時間 */ public static Date getStartDate(Date date) { if (date == null) { return null; } Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); return cal.getTime(); } /** * 根據日期物件來獲取日期中的時間(HH:mm:ss). * * @param date * 日期物件 * @return 時間字串, 格式為: HH:mm:ss */ public static String getTime(Date date) { if (date == null) { return null; } SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss"); return format.format(date); } /** * 根據日期物件來獲取日期中的時間(HH:mm). * * @param date * 日期物件 * @return 時間字串, 格式為: HH:mm */ public static String getTimeIgnoreSecond(Date date) { if (date == null) { return null; } SimpleDateFormat format = new SimpleDateFormat("HH:mm"); return format.format(date); } /** * 判斷是否是閏年 * * @param year * 年份 * @return 是true,否則false */ public static boolean isLeapYear(int year) { Calendar calendar = Calendar.getInstance(); return ((GregorianCalendar) calendar).isLeapYear(year); } /** * 取得一年中的第幾周。 * * @param date * @return */ public static int getWeekOfYear(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); return cal.get(Calendar.WEEK_OF_YEAR); } /** * 獲取上週的指定星期的日期。 * * @param dayOfWeek * 星期幾,取值範圍是 {@link Calendar#MONDAY} - {@link Calendar#SUNDAY} */ public static Date getDateOfPreviousWeek(int dayOfWeek) { if (dayOfWeek > 7 || dayOfWeek < 1) { throw new IllegalArgumentException("引數必須是1-7之間的數字"); } return getDateOfRange(dayOfWeek, -7); } /** * 獲取本週的指定星期的日期。 * * @param dayOfWeek * 星期幾,取值範圍是 {@link Calendar#MONDAY} - {@link Calendar#SUNDAY} */ public static Date getDateOfCurrentWeek(int dayOfWeek) { if (dayOfWeek > 7 || dayOfWeek < 1) { throw new IllegalArgumentException("引數必須是1-7之間的數字"); } return getDateOfRange(dayOfWeek, 0); } /** * 獲取下週的指定星期的日期。 * * @param dayOfWeek * 星期幾,取值範圍是 {@link Calendar#MONDAY} - {@link Calendar#SUNDAY} */ public static Date getDateOfNextWeek(int dayOfWeek) { if (dayOfWeek > 7 || dayOfWeek < 1) { throw new IllegalArgumentException("引數必須是1-7之間的數字"); } return getDateOfRange(dayOfWeek, 7); } private static Date getDateOfRange(int dayOfWeek, int dayOfRange) { Calendar cal = Calendar.getInstance(); cal.set(Calendar.DAY_OF_WEEK, dayOfWeek); cal.set(Calendar.DATE, cal.get(Calendar.DATE) + dayOfRange); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); return cal.getTime(); } public static void main(String[] args) { // Date date =DateUtil.getDate("2017-01-17"); // Date date = new Date(); SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(sf.format(new Date())); } }