1. 程式人生 > >DateUtil 工具類整理一些自己用到過得,有點邏輯處理的方法

DateUtil 工具類整理一些自己用到過得,有點邏輯處理的方法

/** * 獲取當前的年分 * * @return*/public static int getSeasonId() {    Calendar cal = Calendar.getInstance();    return cal.get(Calendar.YEAR);}

/** * 獲得本週一與當前日期相差的天數 * * @return*/public static int getMondayPlus() {    Calendar cd = Calendar.getInstance();    int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK);    if

(dayOfWeek == 1) {return -6;} else {return 2 - dayOfWeek;}}

/** * 獲取當前周每天的開始時間 * @param* @return*/public static List<String> appendBeginDate() {    List<String> beginDateStrs = new ArrayList<>();    for (String str : getWeekdays()) {        String str1 = str + " " + "00:00:01";beginDateStrs.add(str1)

;}return beginDateStrs;}

/** * 獲得當前周     週日  的日期 * * @return*/public static String getPreviousSunday() {return appendEndDate().get(6);}

/** * 獲得當前周   週一的日期 * * @return*/public static String getCurrentMonday() {return appendBeginDate().get(0);}

/** * 獲取當前周每天的開始時間 * * @param* @return*/public static List<String>

appendBeginDate() {    List<String> beginDateStrs = new ArrayList<>();    for (String str : getWeekdays()) {        String str1 = str + " " + "00:00:01";beginDateStrs.add(str1);}return beginDateStrs;}

/** * 獲取當前周每天的結束時間 * * @return*/public static List<String> appendEndDate() {    List<String> endDateStrs = new ArrayList<>();    for (String str : getWeekdays()) {        String str1 = str + " " + "23:59:59";endDateStrs.add(str1);}return endDateStrs;}

/** * 獲取某個時間加上幾分鐘後的時間 * * @param date    時間 * @param minutes 分鐘 * @return*/public static Date getDateAddMinute(Date date, int minutes) {    Calendar calendar = Calendar.getInstance();calendar.setTime(date);calendar.add(Calendar.MINUTE, minutes);    return calendar.getTime();}

/** * 獲取某個時間的n天后的時間 * * @param dateNum n天 * @return*/public static Date getDaysAfterDate(Date date, int dateNum) {    Calendar calendar2 = Calendar.getInstance();calendar2.setTime(date);calendar2.add(Calendar.DATE, 3);    return calendar2.getTime();}

/** * 獲取某個時間的n天前的時間 * * @return*/public static Date getPastDate(int past) {    Calendar calendar = Calendar.getInstance();calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);    return calendar.getTime();}

/** * 獲取兩個時間之間相差多少秒 * * @param startDate 開始時間 * @param startDate 結束時間 * @return*/public static int calLastedTime(Date startDate, Date endDate) {long a = endDate.getTime();    long b = startDate.getTime();    int c = (int) ((a - b) / 1000);    return c;}

/** * 判斷兩個時間相差的天數 */public static int differentDays(Date date1, Date date2) {    Calendar cal1 = Calendar.getInstance();cal1.setTime(date1);Calendar cal2 = Calendar.getInstance();cal2.setTime(date2);    int day1 = cal1.get(Calendar.DAY_OF_YEAR);    int day2 = cal2.get(Calendar.DAY_OF_YEAR);    int year1 = cal1.get(Calendar.YEAR);    int year2 = cal2.get(Calendar.YEAR);    if (year1 != year2)   //同一年{int timeDistance = 0;        for (int i = year1; i < year2; i++) {if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0)    //閏年{                timeDistance += 366;} else    //不是閏年{                timeDistance += 365;}        }return timeDistance + (day2 - day1);} else    //不同年{return day2 - day1;}}

/** * 判斷兩個時間是否在同一天 * * @param date1 時間1 * @param date2 時間2 * @return*/public static boolean isSameDate(Date date1, Date date2) {    Calendar calendar1 = Calendar.getInstance();calendar1.setTime(date1);Calendar calendar2 = Calendar.getInstance();calendar2.setTime(date2);    if (calendar1.get(Calendar.YEAR) != calendar2.get(Calendar.YEAR)) {return false;}if (calendar1.get(Calendar.MONTH) != calendar2.get(Calendar.MONTH)) {return false;}if (calendar1.get(Calendar.DAY_OF_MONTH) != calendar2.get(Calendar.DAY_OF_MONTH)) {return false;}return true;}

/** * 根據生日判斷當前年齡 */public static int getAge(Date birthDay)  {   Calendar cal = Calendar.getInstance();   if (cal.before(birthDay)) {throw new IllegalArgumentException("The birthDay is before Now.It's unbelievable!");}int yearNow = cal.get(Calendar.YEAR);   int monthNow = cal.get(Calendar.MONTH);   int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);cal.setTime(birthDay);   int yearBirth = cal.get(Calendar.YEAR);   int monthBirth = cal.get(Calendar.MONTH);   int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);   int age = yearNow - yearBirth;   if (monthNow <= monthBirth) {if (monthNow == monthBirth) {if (dayOfMonthNow < dayOfMonthBirth) age--;}else{         age--;}   }return age;}

/** * 判斷距離下一個生日還有幾天 */public static int differentBirthDays(Date nowData, Date birthDay) {   SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");Calendar cToday = Calendar.getInstance(); // 存今天Calendar cBirth = Calendar.getInstance(); // 存生日cBirth.setTime(birthDay); // 設定生日cBirth.set(Calendar.YEAR, cToday.get(Calendar.YEAR)); // 修改為本年int days;   if (cBirth.get(Calendar.DAY_OF_YEAR) < cToday.get(Calendar.DAY_OF_YEAR)) {// 生日已經過了,要算明年的了days = cToday.getActualMaximum(Calendar.DAY_OF_YEAR) - cToday.get(Calendar.DAY_OF_YEAR);days += cBirth.get(Calendar.DAY_OF_YEAR);} else {// 生日還沒過days = cBirth.get(Calendar.DAY_OF_YEAR) - cToday.get(Calendar.DAY_OF_YEAR);}// 輸出結果return days;}