java 日期的操作彙總
阿新 • • 發佈:2018-12-19
import java.sql.Timestamp; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.List; import org.apache.commons.lang3.time.DateFormatUtils; public class DateUtils extends org.apache.commons.lang3.time.DateUtils { private static String[] parsePatterns = { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM","yyyy", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM","yyyy", "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM","yyyy"}; /** * 得到當前日期字串 格式(yyyy-MM-dd) */ public static String getDate() { return getDate("yyyy-MM-dd"); } /** * 得到當前日期字串 格式(yyyy-MM-dd) pattern可以為:"yyyy-MM-dd" "HH:mm:ss" "E" */ public static String getDate(String pattern) { return DateFormatUtils.format(new Date(), pattern); } /** * 得到日期字串 預設格式(yyyy-MM-dd) pattern可以為:"yyyy-MM-dd" "HH:mm:ss" "E" */ public static String formatDate(Date date, Object... pattern) { String formatDate = null; if (pattern != null && pattern.length > 0) { formatDate = DateFormatUtils.format(date, pattern[0].toString()); } else { formatDate = DateFormatUtils.format(date, "yyyy-MM-dd"); } return formatDate; } /** * 得到日期時間字串,轉換格式(yyyy-MM-dd HH:mm:ss) */ public static String formatDateTime(Date date) { return formatDate(date, "yyyy-MM-dd HH:mm:ss"); } /** * 得到當前時間字串 格式(HH:mm:ss) */ public static String getTime() { return formatDate(new Date(), "HH:mm:ss"); } /** * 得到當前日期和時間字串 格式(yyyy-MM-dd HH:mm:ss) */ public static String getDateTime() { return formatDate(new Date(), "yyyy-MM-dd HH:mm:ss"); } /** * 得到當前年份字串 格式(yyyy) */ public static String getYear() { return formatDate(new Date(), "yyyy"); } /** * 得到當前月份字串 格式(MM) */ public static String getMonth() { return formatDate(new Date(), "MM"); } /** * 得到當天字串 格式(dd) */ public static String getDay() { return formatDate(new Date(), "dd"); } /** * 得到當前星期字串 格式(E)星期幾 */ public static String getWeek() { return formatDate(new Date(), "E"); } /** * 日期型字串轉化為日期 格式 * { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", * "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", * "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm" } */ public static Date parseDate(Object str) { if (str == null){ return null; } try { return parseDate(str.toString(), parsePatterns); } catch (ParseException e) { return null; } } /** * 獲取過去的天數 * @param date * @return */ public static long pastDays(Date date) { long t = new Date().getTime()-date.getTime(); return t/(24*60*60*1000); } /** * 獲取過去的小時 * @param date * @return */ public static long pastHour(Date date) { long t = new Date().getTime()-date.getTime(); return t/(60*60*1000); } /** * 獲取過去的分鐘 * @param date * @return */ public static long pastMinutes(Date date) { long t = new Date().getTime()-date.getTime(); return t/(60*1000); } /** * 轉換為時間(天,時:分:秒.毫秒) * @param timeMillis * @return */ public static String formatDateTime(long timeMillis){ long day = timeMillis/(24*60*60*1000); long hour = (timeMillis/(60*60*1000)-day*24); long min = ((timeMillis/(60*1000))-day*24*60-hour*60); long s = (timeMillis/1000-day*24*60*60-hour*60*60-min*60); long sss = (timeMillis-day*24*60*60*1000-hour*60*60*1000-min*60*1000-s*1000); return (day>0?day+",":"")+hour+":"+min+":"+s+"."+sss; } /** * 獲取兩個日期之間的天數 * * @param before * @param after * @return */ public static double getDistanceOfTwoDate(Date before, Date after) { long beforeTime = before.getTime(); long afterTime = after.getTime(); return (afterTime - beforeTime) / (1000 * 60 * 60 * 24); } /** * 得到當前時間格式化後時分秒的字串 * @return */ public static String getNowTimeToHMS(){ Date date=new Date(); SimpleDateFormat myFmt=new SimpleDateFormat("HH:mm:ss"); return myFmt.format(date); } /** * 格式化為 HH:mm:ss * @return */ public static String getNowTimeToHMS(Date date){ SimpleDateFormat myFmt=new SimpleDateFormat("HH:mm:ss"); return myFmt.format(date); } /** * yyyy * 得到 年YYYY * @return */ public static String getYYYY(Date date){ SimpleDateFormat myFmt=new SimpleDateFormat("yyyy"); return myFmt.format(date); } /** * yyyy-MM * 得到 年月 * @return */ public static String getYM(Date date){ SimpleDateFormat myFmt=new SimpleDateFormat("yyyy-MM"); return myFmt.format(date); } /** * yyyy-MM-dd * 得到年月日 * @return */ public static String getYMD(Date date){ SimpleDateFormat myFmt=new SimpleDateFormat("yyyy-MM-dd"); return myFmt.format(date); } /** * yyyy-MM-dd HH * 得到當前時間格式化後小時的字串 * @return */ public static String getYMDH(Date date){ SimpleDateFormat myFmt=new SimpleDateFormat("yyyy-MM-dd HH"); return myFmt.format(date); } /** * 格式化為 yyyy-MM-dd HH:mm * @return */ public static String getYMDHM(Date date){ SimpleDateFormat myFmt=new SimpleDateFormat("yyyy-MM-dd HH:mm"); return myFmt.format(date); } /** * 格式化為 yyyy-MM-dd HH:mm:ss * @return */ public static String getYMDHMS(Date date){ SimpleDateFormat myFmt=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return myFmt.format(date); } /** * yyyy-MM-dd-HH-mm-ss * @param date * @return */ public static String getYMDHMS_(Date date){ SimpleDateFormat myFmt=new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss"); return myFmt.format(date); } /** * yyyy * 得到 年YYYY * @return */ public static Date getDateYYYY(String source){ SimpleDateFormat myFmt=new SimpleDateFormat("yyyy"); try { return myFmt.parse(source); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } /** * yyyy-MM * 得到 年月 * @return */ public static Date getDateYM(String source){ SimpleDateFormat myFmt=new SimpleDateFormat("yyyy-MM"); try { return myFmt.parse(source); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } /** * yyyy-MM-dd * 得到年月日 * @return */ public static Date getDateYMD(String source){ SimpleDateFormat myFmt=new SimpleDateFormat("yyyy-MM-dd"); try { return myFmt.parse(source); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } /** * yyyy-MM-dd HH * 得到當前時間格式化後小時的字串 * @return */ public static Date getDateYMDH(String source){ SimpleDateFormat myFmt=new SimpleDateFormat("yyyy-MM-dd HH"); try { return myFmt.parse(source); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } /** * 格式化為 yyyy-MM-dd HH:mm * @return */ public static Date getDateYMDHM(String source){ SimpleDateFormat myFmt=new SimpleDateFormat("yyyy-MM-dd HH:mm"); try { return myFmt.parse(source); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } /** * 格式化為 yyyy-MM-dd HH:mm:ss * @return */ public static Date getDateYMDHMS(String source){ SimpleDateFormat myFmt=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { return myFmt.parse(source); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } public static String getNowTime(){ Date date=new Date(); SimpleDateFormat myFmt=new SimpleDateFormat("yyMMddHHmmss"); return myFmt.format(date); } /** * 得到本日第一個小時 * @param date * @return */ public static Date getThisDayFirstHour(Date date){ Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.set(Calendar.HOUR_OF_DAY,cal.getActualMinimum(Calendar.HOUR_OF_DAY)); return cal.getTime(); } /** * 得到本日最後一個小時 * @param date * @return */ public static Date getThisDayLastHour(Date date){ Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.set(Calendar.HOUR_OF_DAY,cal.getActualMaximum(Calendar.HOUR_OF_DAY)); return cal.getTime(); } /** * 獲得上個月的第一天日期 * yyyy-MM-dd * @param date * @return */ public static Date getLastMonthFirstDay(Date date){ Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.add(Calendar.MONTH, -1); cal.set(Calendar.DAY_OF_MONTH,cal.getActualMinimum(Calendar.DAY_OF_MONTH)); return cal.getTime(); } /** * 上個月最後一天 * @param date * @return */ public static Date getLastMonthLastDay(Date date){ Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.add(Calendar.MONTH, -1); cal.set(Calendar.DAY_OF_MONTH,cal.getActualMaximum(Calendar.DAY_OF_MONTH)); return cal.getTime(); } /** * 得到本月第一天 日期 * @param date * @return */ public static Date getThisMonthFirstDay(Date date){ Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.set(Calendar.DAY_OF_MONTH,cal.getActualMinimum(Calendar.DAY_OF_MONTH)); return cal.getTime(); } /** * 得到本月最後一天 日期 * @param date * @return */ public static Date getThisMonthLastDay(Date date){ Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.set(Calendar.DAY_OF_MONTH,cal.getActualMaximum(Calendar.DAY_OF_MONTH)); return cal.getTime(); } /** * 得到前一天 日期 * @param date * @return */ public static Date getLastDay(Date date){ Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.add(Calendar.DAY_OF_MONTH, -1); return cal.getTime(); } /** * 得到下一天 日期 * @param date * @return */ public static Date getNextDay(Date date){ Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.add(Calendar.DAY_OF_MONTH, 1); return cal.getTime(); } /** * 得到上個月月份 * yyyy-MM * @param date * @return */ public static Date getLastMonth(Date date){ Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.add(Calendar.MONTH, -1); return cal.getTime(); } /** * 得到上一年 年份 * @param date * @return */ public static Date getLastYear(Date date){ Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.add(Calendar.YEAR, -1); return cal.getTime(); } /** * 得到本年最後一個月 * @param date * @return */ public static Date getThisYearLastMonth(Date date){ Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.set(Calendar.MONTH,cal.getActualMaximum(Calendar.MONTH)); return cal.getTime(); } /** * 得到本年第一個月 * @param date * @return */ public static Date getThisYearFirstMonth(Date date){ Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.set(Calendar.MONTH,cal.getActualMinimum(Calendar.MONTH)); return cal.getTime(); } /** * 獲得上月最後一天的日期 * yyyy-MM-dd * @param date * @return */ public static Date getLastMonthEndDay(Date date){ Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.add(Calendar.MONTH, -1); cal.set(Calendar.DAY_OF_MONTH,cal.getActualMaximum(Calendar.DAY_OF_MONTH)); return cal.getTime(); } /** * 獲得上一年一月日期 * yyyy-MM * @param date * @return */ public static Date getLastYearFirstMonth(Date date){ Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.add(Calendar.YEAR, -1); cal.set(Calendar.DAY_OF_YEAR,cal.getActualMinimum(Calendar.DAY_OF_YEAR)); return cal.getTime(); } /** * 獲得上一年12月日期 * yyyy-MM * @param date * @return */ public static Date getLastYearEndMonth(Date date){ Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.add(Calendar.YEAR, -1); cal.set(Calendar.DAY_OF_YEAR,cal.getActualMaximum(Calendar.DAY_OF_YEAR)); return cal.getTime(); } /** * 獲取上週一 * @param date * @return */ public static Date getLastWeekMonday(Date date) { Date a = DateUtils.addDays(date, -1); Calendar cal = Calendar.getInstance(); cal.setTime(a); cal.add(Calendar.WEEK_OF_YEAR, -1);// 一週 cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); return cal.getTime(); } /** * 獲取上週日 * @param date * @return */ public static Date getLastWeekSunday(Date date) { Date a = DateUtils.addDays(date, -1); Calendar cal = Calendar.getInstance(); cal.setTime(a); cal.set(Calendar.DAY_OF_WEEK, 1); return cal.getTime(); } //獲取當天的開始時間 public static java.util.Date getDayBegin() { Calendar cal = new GregorianCalendar(); 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 java.util.Date getDayEnd() { Calendar cal = new GregorianCalendar(); cal.set(Calendar.HOUR_OF_DAY, 23); cal.set(Calendar.MINUTE, 59); cal.set(Calendar.SECOND, 59); return cal.getTime(); } //獲取昨天的開始時間 public static Date getBeginDayOfYesterday() { Calendar cal = new GregorianCalendar(); cal.setTime(getDayBegin()); cal.add(Calendar.DAY_OF_MONTH, -1); return cal.getTime(); } //獲取昨天的結束時間 public static Date getEndDayOfYesterDay() { Calendar cal = new GregorianCalendar(); cal.setTime(getDayEnd()); cal.add(Calendar.DAY_OF_MONTH, -1); return cal.getTime(); } //獲取明天的開始時間 public static Date getBeginDayOfTomorrow() { Calendar cal = new GregorianCalendar(); cal.setTime(getDayBegin()); cal.add(Calendar.DAY_OF_MONTH, 1); return cal.getTime(); } //獲取明天的結束時間 public static Date getEndDayOfTomorrow() { Calendar cal = new GregorianCalendar(); cal.setTime(getDayEnd()); cal.add(Calendar.DAY_OF_MONTH, 1); return cal.getTime(); } //獲取本週的開始時間 @SuppressWarnings("unused") public static Date getBeginDayOfWeek() { Date date = new Date(); if (date == null) { return null; } Calendar cal = Calendar.getInstance(); cal.setTime(date); int dayofweek = cal.get(Calendar.DAY_OF_WEEK); if (dayofweek == 1) { dayofweek += 7; } cal.add(Calendar.DATE, 2 - dayofweek); return getDayStartTime(cal.getTime()); } //獲取本週的結束時間 public static Date getEndDayOfWeek(){ Calendar cal = Calendar.getInstance(); cal.setTime(getBeginDayOfWeek()); cal.add(Calendar.DAY_OF_WEEK, 6); Date weekEndSta = cal.getTime(); return getDayEndTime(weekEndSta); } //獲取上週的開始時間 @SuppressWarnings("unused") public static Date getBeginDayOfLastWeek() { Date date = new Date(); if (date == null) { return null; } Calendar cal = Calendar.getInstance(); cal.setTime(date); int dayofweek = cal.get(Calendar.DAY_OF_WEEK); if (dayofweek == 1) { dayofweek += 7; } cal.add(Calendar.DATE, 2 - dayofweek - 7); return getDayStartTime(cal.getTime()); } //獲取上週的結束時間 public static Date getEndDayOfLastWeek(){ Calendar cal = Calendar.getInstance(); cal.setTime(getBeginDayOfLastWeek()); cal.add(Calendar.DAY_OF_WEEK, 6); Date weekEndSta = cal.getTime(); return getDayEndTime(weekEndSta); } //獲取本月的開始時間 public static Date getBeginDayOfMonth() { Calendar calendar = Calendar.getInstance(); calendar.set(getNowYear(), getNowMonth() - 1, 1); return getDayStartTime(calendar.getTime()); } //獲取本月的結束時間 public static Date getEndDayOfMonth() { Calendar calendar = Calendar.getInstance(); calendar.set(getNowYear(), getNowMonth() - 1, 1); int day = calendar.getActualMaximum(5); calendar.set(getNowYear(), getNowMonth() - 1, day); return getDayEndTime(calendar.getTime()); } //獲取上月的開始時間 public static Date getBeginDayOfLastMonth() { Calendar calendar = Calendar.getInstance(); calendar.set(getNowYear(), getNowMonth() - 2, 1); return getDayStartTime(calendar.getTime()); } //獲取上月的結束時間 public static Date getEndDayOfLastMonth() { Calendar calendar = Calendar.getInstance(); calendar.set(getNowYear(), getNowMonth() - 2, 1); int day = calendar.getActualMaximum(5); calendar.set(getNowYear(), getNowMonth() - 2, day); return getDayEndTime(calendar.getTime()); } //獲取本年的開始時間 public static java.util.Date getBeginDayOfYear() { Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, getNowYear()); // cal.set cal.set(Calendar.MONTH, Calendar.JANUARY); cal.set(Calendar.DATE, 1); return getDayStartTime(cal.getTime()); } //獲取本年的結束時間 public static java.util.Date getEndDayOfYear() { Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, getNowYear()); cal.set(Calendar.MONTH, Calendar.DECEMBER); cal.set(Calendar.DATE, 31); return getDayEndTime(cal.getTime()); } //獲取某個日期的開始時間 public static Timestamp getDayStartTime(Date d) { Calendar calendar = Calendar.getInstance(); if(null != d) calendar.setTime(d); calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0); calendar.set(Calendar.MILLISECOND, 0); return new Timestamp(calendar.getTimeInMillis()); } //獲取某個日期的結束時間 public static Timestamp getDayEndTime(Date d) { Calendar calendar = Calendar.getInstance(); if(null != d) calendar.setTime(d); calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59); calendar.set(Calendar.MILLISECOND, 999); return new Timestamp(calendar.getTimeInMillis()); } //獲取今年是哪一年 public static Integer getNowYear() { Date date = new Date(); GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance(); gc.setTime(date); return Integer.valueOf(gc.get(1)); } //獲取本月是哪一月 public static int getNowMonth() { Date date = new Date(); GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance(); gc.setTime(date); return gc.get(2) + 1; } //兩個日期相減得到的天數 public static int getDiffDays(Date beginDate, Date endDate) { if (beginDate == null || endDate == null) { throw new IllegalArgumentException("getDiffDays param is null!"); } long diff = (endDate.getTime() - beginDate.getTime()) / (1000 * 60 * 60 * 24); int days = new Long(diff).intValue(); return days; } //兩個日期相減得到的毫秒數 public static long dateDiff(Date beginDate, Date endDate) { long date1ms = beginDate.getTime(); long date2ms = endDate.getTime(); return date2ms - date1ms; } //獲取兩個日期中的最大日期 public static Date max(Date beginDate, Date endDate) { if (beginDate == null) { return endDate; } if (endDate == null) { return beginDate; } if (beginDate.after(endDate)) { return beginDate; } return endDate; } //獲取兩個日期中的最小日期 public static Date min(Date beginDate, Date endDate) { if (beginDate == null) { return endDate; } if (endDate == null) { return beginDate; } if (beginDate.after(endDate)) { return endDate; } return beginDate; } //返回某月該季度的第一個月 public static Date getFirstSeasonDate(Date date) { final int[] SEASON = { 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4 }; Calendar cal = Calendar.getInstance(); cal.setTime(date); int sean = SEASON[cal.get(Calendar.MONTH)]; cal.set(Calendar.MONTH, sean * 3 - 3); return cal.getTime(); } //返回某個日期下幾天的日期 public static Date getNextDay(Date date, int i) { Calendar cal = new GregorianCalendar(); cal.setTime(date); cal.set(Calendar.DATE, cal.get(Calendar.DATE) + i); return cal.getTime(); } //返回某個日期前幾天的日期 public static Date getFrontDay(Date date, int i) { Calendar cal = new GregorianCalendar(); cal.setTime(date); cal.set(Calendar.DATE, cal.get(Calendar.DATE) - i); return cal.getTime(); } //獲取某年某月到某年某月按天的切片日期集合(間隔天數的集合) @SuppressWarnings({ "rawtypes", "unchecked" }) public static List getTimeList(int beginYear, int beginMonth, int endYear, int endMonth, int k) { List list = new ArrayList(); if (beginYear == endYear) { for (int j = beginMonth; j <= endMonth; j++) { list.add(getTimeList(beginYear, j, k)); } } else { { for (int j = beginMonth; j < 12; j++) { list.add(getTimeList(beginYear, j, k)); } for (int i = beginYear + 1; i < endYear; i++) { for (int j = 0; j < 12; j++) { list.add(getTimeList(i, j, k)); } } for (int j = 0; j <= endMonth; j++) { list.add(getTimeList(endYear, j, k)); } } } return list; } //獲取某年某月按天切片日期集合(某個月間隔多少天的日期集合) @SuppressWarnings({ "unchecked", "rawtypes" }) public static List getTimeList(int beginYear, int beginMonth, int k) { List list = new ArrayList(); Calendar begincal = new GregorianCalendar(beginYear, beginMonth, 1); int max = begincal.getActualMaximum(Calendar.DATE); for (int i = 1; i < max; i = i + k) { list.add(begincal.getTime()); begincal.add(Calendar.DATE, k); } begincal = new GregorianCalendar(beginYear, beginMonth, max); list.add(begincal.getTime()); return list; } }