java DateUtils日期时间工具类

主要包含如下功能:

1、日期时间格式转换、

2、日期时间比较

3、向指定日期的前后,加减 n年、n月、n天、n小时、n分钟、n秒钟

4、日期时间对象与日期时间字符串互转

更多功能详见代码:

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class DateUtils {

   private static Log logger = LogFactory.getLog(DateUtils.class);

   public static final String YYYY_MM = "yyyy-MM";
   public static final String YYYY_MM_DD = "yyyy-MM-dd";
   public static final String YYYY_MM_DD_HH = "yyyy-MM-dd HH";
   public static final String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
   public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
   public static final String YYYY_MM_DD_HH_MM_SS_SSS = "yyyy-MM-dd HH:mm:ss.SSS";
   public static final String YYYYMMDD = "yyyyMMdd";
   public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
   public static final String YYMM = "yyMM";
   public static final String HH_MM_SS = "HH:mm:ss";
   public static final String YYYY年MM月 = "yyyy年MM月";
   public static final String DAY_LAST_TIME = "23:59:59";
   public static final String DAY_FIRST_TIME = "00:00:00";
   public static final int ONE_DAY_SECONDS = 86400;
   public static final int ONE_HOUR_SECONDS = 3600;
   public static final int ONE_MINUTE_SECONDS = 60;
   private static final int[] MONTH_DAYS = new int[]{29, 30};
   private static final int[][] DAYS_MONTH = new int[][]{{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
   private static final int[][] DATAS = new int[][]{{23, 3, 2, 17, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0}, {41, 0, 4, 23, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1}, {30, 7, 5, 28, 1, 1, 0, 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23, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1}, {46, 0, 0, 28, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0}, {35, 0, 1, 33, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0}, {24, 4, 2, 38, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1}, {42, 0, 4, 44, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1}, {31, 0, 5, 49, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0}, {21, 2, 6, 54, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1}, {40, 0, 0, 59, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1}, {28, 6, 2, 5, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0}, {47, 0, 3, 10, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1}, {36, 0, 4, 15, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1}, {25, 5, 5, 20, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0}, {43, 0, 0, 26, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1}, {32, 0, 1, 31, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0}, {22, 3, 2, 36, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0}};

   public DateUtils() {
   }

   public static Date getGregorianCalendar(String date) {
      Calendar c = getCalendar(date);
      int year = c.get(1);
      int month = c.get(2) + 1;
      int day = c.get(5);
      int index = year - 1936;
      int endIndex = month;
      if (DATAS[index][1] != 0 && month > DATAS[index][1]) {
         endIndex = month + 1;
      }

      int dayNum = 0;

      for(int i = 0; i < endIndex - 1; ++i) {
         dayNum += MONTH_DAYS[DATAS[index][4 + i]];
      }

      dayNum += day;
      dayNum += DATAS[index][0];
      int year_days = 365;
      if (isLeapYear(year)) {
         year_days = 366;
      }

      if (dayNum > year_days) {
         ++year;
         dayNum -= year_days;
      }

      month = 1;
      int[] dayOfMonth = DAYS_MONTH[0];
      if (isLeapYear(year)) {
         dayOfMonth = DAYS_MONTH[1];
      }

      int i;
      for(i = 0; i < 12; ++i) {
         dayNum -= dayOfMonth[i];
         if (dayNum <= 0) {
            break;
         }

         ++month;
      }

      day = dayOfMonth[i] + dayNum;
      return getDate(year + "-" + month + "-" + day);
   }

   public static String formatPrettySecond(int second) {
      int days = second / 86400;
      int modSecond = second % 86400;
      int hours = modSecond / 3600;
      modSecond %= 3600;
      int minutes = modSecond / 60;
      modSecond %= 60;
      StringBuilder sb = new StringBuilder();
      if (days > 0) {
         sb.append(days).append("天");
      }

      if (hours > 0) {
         sb.append(hours).append("小时");
      }

      if (minutes > 0) {
         sb.append(minutes).append("分");
      }

      if (modSecond > 0) {
         sb.append(modSecond).append("秒");
      }

      return sb.toString();
   }

   public static String formatForDisplaySecond(int second) {
      int days = second / 86400;
      int modSecond = second % 86400;
      int hours = modSecond / 3600;
      modSecond %= 3600;
      int minutes = modSecond / 60;
      modSecond %= 60;
      StringBuilder sb = new StringBuilder();
      if (days > 0) {
         sb.append(days).append("天");
         if (hours > 0) {
            sb.append(hours).append("小时");
         }
      } else if (hours > 0) {
         sb.append(hours).append("小时");
         if (minutes > 0) {
            sb.append(minutes).append("分");
         }
      } else if (minutes > 0) {
         sb.append(minutes).append("分");
         if (modSecond > 0) {
            sb.append(modSecond).append("秒");
         }
      } else {
         sb.append(modSecond).append("秒");
      }

      return sb.toString();
   }

   public static Date getLunarCalendar(String date) {
      Calendar calendar = getCalendar(date);
      int year = calendar.get(1);
      int month = 1;
      if (year >= 1936 && year <= 2028) {
         int index = year - 1936;
         int l_days = DATAS[index][0];
         int day_year = calendar.get(6);
         int days;
         if (day_year >= l_days) {
            days = day_year - l_days;
         } else {
            --index;
            --year;
            Calendar c = getCalendar(year + "-12-31");
            days = c.get(6) + day_year - DATAS[index][0];
         }

         int i = 0;

         int day_num;
         for(day_num = 0; i < 13; ++i) {
            day_num += MONTH_DAYS[DATAS[index][i + 4]];
            if (day_num >= days) {
               break;
            }

            ++month;
         }

         int day = MONTH_DAYS[DATAS[index][i + 4]] - (day_num - days);
         if (DATAS[index][1] != 0 && month > DATAS[index][1]) {
            --month;
         }

         return getDate(year + "-" + month + "-" + day);
      } else {
         return null;
      }
   }

   public static Date getDate(String date) {
      DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
      Date dd = null;

      try {
         dd = format.parse(date);
      } catch (Exception var4) {
         var4.printStackTrace();
      }

      return dd;
   }

   public static boolean isLeapYear(int year) {
      if (year % 400 == 0) {
         return true;
      } else if (year % 100 == 0) {
         return false;
      } else {
         return year % 4 == 0;
      }
   }

   public static String format(Date date) {
      return format(date, "yyyy-MM-dd");
   }

   public static String format(Date date, String fmt) {
      if (date == null) {
         return "";
      } else {
         SimpleDateFormat sdf = new SimpleDateFormat(fmt);
         return sdf.format(date);
      }
   }

   public static Date parse(String dateStr) {
      String fmt = getFmtStr(dateStr);
      if (StrUtils.empty(fmt)) {
         return null;
      } else {
         try {
            SimpleDateFormat sdf = new SimpleDateFormat(fmt);
            return sdf.parse(dateStr);
         } catch (Exception var3) {
            logger.error("转换日期出错:" + var3.getMessage(), var3);
            return null;
         }
      }
   }

   public static Date parse(Integer time, Date def) {
      if (time == null) {
         return def;
      } else {
         Date date = new Date();
         date.setTime((long)time);
         return date;
      }
   }

   public static long toTime(Date d, long def) {
      return d == null ? def : d.getTime();
   }

   public static String getFmtStr(String dateStr) {
      if (StrUtils.isEmpty(dateStr)) {
         return "";
      } else {
         int len = dateStr.trim().length();
         if (len == 10) {
            return "yyyy-MM-dd";
         } else if (len == 19) {
            return "yyyy-MM-dd HH:mm:ss";
         } else if (len >= 21 && len <= 23) {
            return "yyyy-MM-dd HH:mm:ss.SSS";
         } else if (len == 7) {
            return "yyyy-MM";
         } else if (len == 8) {
            return "HH:mm:ss";
         } else if (len == 13) {
            return "yyyy-MM-dd HH";
         } else if (len == 14) {
            return "yyyyMMddHHmmss";
         } else {
            return len == 16 ? "yyyy-MM-dd HH:mm" : "";
         }
      }
   }

   public static Date addMonths(Date date, int month) {
      Calendar cal = getCalendar(date);
      cal.add(2, month);
      return cal.getTime();
   }

   public static Date addYears(Date date, int year) {
      Calendar cal = getCalendar(date);
      cal.add(1, year);
      return cal.getTime();
   }

   public static Date addDays(Date date, int day) {
      Calendar cal = getCalendar(date);
      cal.add(5, day);
      return cal.getTime();
   }

   public static Date addHour(Date date, int hour) {
      Calendar cal = getCalendar(date);
      cal.add(11, hour);
      return cal.getTime();
   }

   public static Date addMinute(Date date, int minute) {
      Calendar cal = getCalendar(date);
      cal.add(12, minute);
      return cal.getTime();
   }

   public static Date addSecond(Date date, int second) {
      Calendar cal = getCalendar(date);
      cal.add(13, second);
      return cal.getTime();
   }

   public static Calendar getCalendar(Date date) {
      Calendar calendar = Calendar.getInstance();
      if (date != null) {
         calendar.setTime(date);
      }

      return calendar;
   }

   public static Calendar getCalendar(String date) {
      Date d = parse(date);
      return getCalendar(d);
   }

   public static int getMaxDayOfLastMonth() {
      Date now = new Date();
      Date lastMonth = addMonths(now, -1);
      lastMonth = getMaxDateOfMonth(lastMonth);
      Calendar calendar = Calendar.getInstance();
      calendar.setTime(lastMonth);
      int maxDay = calendar.get(5);
      return maxDay;
   }

   public static int getYearOfLastMonth() {
      Calendar calendar = Calendar.getInstance();
      calendar.add(2, -1);
      int yearOfLastMonth = calendar.get(1);
      return yearOfLastMonth;
   }

   public static int getMonthOfLastMonth() {
      Calendar calendar = Calendar.getInstance();
      calendar.add(2, -1);
      int lastMonth = calendar.get(2) + 1;
      return lastMonth;
   }

   public static int getCurrentYear() {
      Calendar cal = Calendar.getInstance();
      int year = cal.get(1);
      return year;
   }

   public static int getCurrentMonth() {
      Calendar cal = Calendar.getInstance();
      int month = cal.get(2) + 1;
      return month;
   }

   public static int getCurrentDay() {
      Calendar cal = Calendar.getInstance();
      int day = cal.get(5);
      return day;
   }

   public static Date validateStartDate(Date startDate) {
      Date today = new Date();
      if (startDate.compareTo(today) == 1) {
         startDate = today;
      }

      return startDate;
   }

   public static Date notAfterNow(Date myDate) {
      Date today = new Date();
      if (myDate.after(today)) {
         myDate = today;
      }

      return myDate;
   }

   public static Date notAfterYesterday(Date myDate) {
      Date today = new Date();
      Date yesterday = addDays(today, -1);
      if (myDate.after(yesterday)) {
         myDate = yesterday;
      }

      return myDate;
   }

   public static Date notAfterLastMonth(Date myDate) {
      Date today = new Date();
      Date lastMonth = addMonths(today, -1);
      lastMonth = getMaxDateOfMonth(lastMonth);
      if (myDate.after(lastMonth)) {
         myDate = lastMonth;
      }

      return myDate;
   }

   public static Date notAfterLastYear(Date myDate) {
      Date today = new Date();
      Date lastYear = addYears(today, -1);
      lastYear = getMaxDateOfYear(lastYear);
      if (myDate.after(lastYear)) {
         myDate = lastYear;
      }

      return myDate;
   }

   public static Date notBefore(Date myDate, String basicStr) throws Exception {
      Date basicDate = parse(basicStr);
      if (myDate.before(basicDate)) {
         myDate = basicDate;
      }

      return myDate;
   }

   public static Date toDate(int year, int month, int day) {
      Calendar calendar = Calendar.getInstance();
      calendar.set(year, month, day);
      return calendar.getTime();
   }

   public static String StringPatternToPattern(String str, String pattern1, String pattern2) {
      Date dateTime = null;
      String productStr = "";

      try {
         if (str != null && !str.equals("")) {
            SimpleDateFormat formater = new SimpleDateFormat(pattern1);
            dateTime = formater.parse(str);
            SimpleDateFormat formater1 = new SimpleDateFormat(pattern2);
            productStr = formater1.format(dateTime);
         }
      } catch (Exception var7) {
         logger.error(var7);
      }

      return productStr;
   }

   public static Date now() {
      return new Date();
   }

   public static String nowStr() {
      return format(now(), "yyyy-MM-dd HH:mm:ss");
   }

   public static String nowStr(String fmt) {
      return format(now(), fmt);
   }

   public static Date getMinDateOfHour(Date date) {
      if (date == null) {
         return null;
      } else {
         Calendar calendar = Calendar.getInstance();
         calendar.setTime(date);
         calendar.set(12, calendar.getActualMinimum(12));
         calendar.set(13, calendar.getActualMinimum(13));
         calendar.set(14, calendar.getActualMinimum(14));
         return calendar.getTime();
      }
   }

   public static Date getMinDateOfDay(Date date) {
      if (date == null) {
         return null;
      } else {
         Calendar calendar = Calendar.getInstance();
         calendar.setTime(getMinDateOfHour(date));
         calendar.set(11, calendar.getActualMinimum(11));
         return calendar.getTime();
      }
   }

   public static Date getMinDateOfYear(Date date) {
      if (date == null) {
         return null;
      } else {
         Calendar calendar = Calendar.getInstance();
         calendar.setTime(date);
         calendar.set(6, calendar.getActualMinimum(6));
         calendar.set(11, calendar.getActualMinimum(11));
         calendar.set(12, calendar.getActualMinimum(12));
         calendar.set(13, calendar.getActualMinimum(13));
         calendar.set(14, calendar.getActualMinimum(14));
         return calendar.getTime();
      }
   }

   public static Date getMaxDateOfYear(Date date) {
      if (date == null) {
         return null;
      } else {
         Calendar calendar = Calendar.getInstance();
         calendar.setTime(date);
         calendar.set(6, calendar.getActualMaximum(6));
         calendar.set(11, calendar.getActualMaximum(11));
         calendar.set(12, calendar.getActualMaximum(12));
         calendar.set(13, calendar.getActualMaximum(13));
         calendar.set(14, calendar.getActualMaximum(14));
         return calendar.getTime();
      }
   }

   public static Date getMinDateOfWeek(Date date, Locale locale) {
      if (date == null) {
         return null;
      } else {
         Calendar calendar = Calendar.getInstance();
         int day_of_week = calendar.get(7);
         calendar.setTime(date);
         calendar.set(7, calendar.getActualMinimum(7));
         calendar.set(11, calendar.getActualMinimum(11));
         calendar.set(12, calendar.getActualMinimum(12));
         calendar.set(13, calendar.getActualMinimum(13));
         calendar.set(14, calendar.getActualMinimum(14));
         if (locale == null) {
            locale = Locale.CHINESE;
         }

         Date tmpDate = calendar.getTime();
         if (Locale.CHINESE.getLanguage().equals(locale.getLanguage())) {
            if (day_of_week == 1) {
               tmpDate = addDays(tmpDate, -6);
            } else {
               tmpDate = addDays(tmpDate, 1);
            }
         }

         return tmpDate;
      }
   }

   public static Date getMaxDateOfWeek(Date date, Locale locale) {
      if (date == null) {
         return null;
      } else {
         Calendar calendar = Calendar.getInstance();
         int day_of_week = calendar.get(7);
         calendar.setTime(date);
         calendar.set(7, calendar.getActualMaximum(7));
         calendar.set(11, calendar.getActualMaximum(11));
         calendar.set(12, calendar.getActualMaximum(12));
         calendar.set(13, calendar.getActualMaximum(13));
         calendar.set(14, calendar.getActualMaximum(14));
         if (locale == null) {
            locale = Locale.CHINESE;
         }

         Date tmpDate = calendar.getTime();
         if (Locale.CHINESE.getLanguage().equals(locale.getLanguage())) {
            if (day_of_week == 1) {
               tmpDate = addDays(tmpDate, -6);
            } else {
               tmpDate = addDays(tmpDate, 1);
            }
         }

         return tmpDate;
      }
   }

   public static Date getMinDateOfMonth(Date date) {
      if (date == null) {
         return null;
      } else {
         Calendar calendar = Calendar.getInstance();
         calendar.setTime(date);
         calendar.set(5, calendar.getActualMinimum(5));
         calendar.set(11, calendar.getActualMinimum(11));
         calendar.set(12, calendar.getActualMinimum(12));
         calendar.set(13, calendar.getActualMinimum(13));
         calendar.set(14, calendar.getActualMinimum(14));
         return calendar.getTime();
      }
   }

   public static Date getMaxDateOfMonth(Date date) {
      if (date == null) {
         return null;
      } else {
         Calendar calendar = Calendar.getInstance();
         calendar.setTime(date);
         calendar.set(5, calendar.getActualMaximum(5));
         calendar.set(11, calendar.getActualMaximum(11));
         calendar.set(12, calendar.getActualMaximum(12));
         calendar.set(13, calendar.getActualMaximum(13));
         calendar.set(14, calendar.getActualMaximum(14));
         return calendar.getTime();
      }
   }

   public static Date getMaxDateOfDay(Date date) {
      if (date == null) {
         return null;
      } else {
         Calendar calendar = Calendar.getInstance();
         calendar.setTime(date);
         calendar.set(11, calendar.getActualMaximum(11));
         calendar.set(12, calendar.getActualMaximum(12));
         calendar.set(13, calendar.getActualMaximum(13));
         calendar.set(14, calendar.getActualMaximum(14));
         return calendar.getTime();
      }
   }

   public static Date getMaxDateOfHour(Date date) {
      if (date == null) {
         return null;
      } else {
         Calendar calendar = Calendar.getInstance();
         calendar.setTime(date);
         calendar.set(12, calendar.getActualMaximum(12));
         calendar.set(13, calendar.getActualMaximum(13));
         calendar.set(14, calendar.getActualMaximum(14));
         return calendar.getTime();
      }
   }

   public static int getIntervalSecond(Date startDate, Date endDate) {
      if (startDate != null && endDate != null) {
         if (startDate.after(endDate)) {
            Date tmp = endDate;
            endDate = startDate;
            startDate = tmp;
         }

         long timeNumber = -1L;
         long TIME = 1000L;

         try {
            timeNumber = (endDate.getTime() - startDate.getTime()) / TIME;
         } catch (Exception var7) {
            logger.error(var7);
         }

         return (int)timeNumber;
      } else {
         return -1;
      }
   }

   public static int getIntervalMinute(Date startDate, Date endDate) {
      if (startDate != null && endDate != null) {
         if (startDate.after(endDate)) {
            Date tmp = endDate;
            endDate = startDate;
            startDate = tmp;
         }

         long timeNumber = -1L;
         long TIME = 60000L;

         try {
            timeNumber = (endDate.getTime() - startDate.getTime()) / TIME;
         } catch (Exception var7) {
            logger.error(var7, var7);
         }

         return (int)timeNumber;
      } else {
         return -1;
      }
   }

   public static int getIntervalHour(Date startDate, Date endDate) {
      if (startDate != null && endDate != null) {
         if (startDate.after(endDate)) {
            Date tmp = endDate;
            endDate = startDate;
            startDate = tmp;
         }

         long timeNumber = -1L;
         long TIME = 3600000L;

         try {
            timeNumber = (endDate.getTime() - startDate.getTime()) / TIME;
         } catch (Exception var7) {
            var7.printStackTrace();
         }

         return (int)timeNumber;
      } else {
         return -1;
      }
   }

   public static int getIntervalDay(Date startDate, Date endDate) {
      if (startDate != null && endDate != null) {
         if (startDate.after(endDate)) {
            Date tmp = endDate;
            endDate = startDate;
            startDate = tmp;
         }

         long dayNumber = -1L;
         long DAY = 86400000L;

         try {
            dayNumber = (endDate.getTime() + 1000L - startDate.getTime()) / DAY;
         } catch (Exception var7) {
            logger.error(var7, var7);
         }

         return (int)dayNumber;
      } else {
         return -1;
      }
   }

   public static int getIntervalMonth(Date startDate, Date endDate) {
      int result = 0;

      try {
         if (startDate == null || endDate == null) {
            return -1;
         }

         if (startDate.after(endDate)) {
            Date tmp = endDate;
            endDate = startDate;
            startDate = tmp;
         }

         Calendar calendar = Calendar.getInstance();
         calendar.setTime(startDate);
         int monthS = calendar.get(2);
         int yearS = calendar.get(1);
         calendar.setTime(endDate);
         int monthE = calendar.get(2);
         int yearE = calendar.get(1);
         if (yearE - yearS == 0) {
            result = monthE - monthS;
         } else {
            result = (yearE - yearS - 1) * 12 + (12 - monthS) + monthE;
         }
      } catch (Exception var8) {
         logger.error(var8);
      }

      return result;
   }

   public static int getIntervalYear(Date startDate, Date endDate) {
      int result = 0;

      try {
         if (startDate == null || endDate == null) {
            return -1;
         }

         if (startDate.after(endDate)) {
            Date tmp = endDate;
            endDate = startDate;
            startDate = tmp;
         }

         Calendar calendar = Calendar.getInstance();
         calendar.setTime(startDate);
         int yearS = calendar.get(1);
         calendar.setTime(endDate);
         int yearE = calendar.get(1);
         result = yearE - yearS;
      } catch (Exception var6) {
         logger.error(var6);
      }

      return result;
   }

   public static int getAge(String birthday) {
      Date d = parse(birthday);
      if (d == null) {
         return 0;
      } else {
         int age = getIntervalYear(new Date(), d);
         return Math.abs(age);
      }
   }

   public static List<Date> splitDateByDay(Date startDate, Date endDate) {
      if (startDate != null && endDate != null) {
         List<Date> dateList = new ArrayList();
         dateList.add(startDate);
         int num = getIntervalDay(startDate, endDate);

         for(int i = 1; i <= num; ++i) {
            dateList.add(addDays(startDate, i));
         }

         return dateList;
      } else {
         return null;
      }
   }

   public static List<Date> splitDateByMonth(Date startDate, Date endDate) {
      List<Date> dateList = new ArrayList();
      if (startDate != null && endDate != null) {
         dateList.add(startDate);
         int num = getIntervalMonth(startDate, endDate);

         for(int i = 1; i <= num; ++i) {
            dateList.add(addMonths(startDate, i));
         }

         return dateList;
      } else {
         return dateList;
      }
   }

   public static List<Date> splitDateByYear(Date startDate, Date endDate) {
      List<Date> dateList = new ArrayList();
      if (startDate != null && endDate != null) {
         dateList.add(startDate);
         int num = getIntervalYear(startDate, endDate);

         for(int i = 1; i <= num; ++i) {
            dateList.add(addYears(startDate, i));
         }

         return dateList;
      } else {
         return dateList;
      }
   }

   public static List<Date> getCurrentQuarter() {
      List<Date> dateList = new ArrayList();
      Date date = new Date(System.currentTimeMillis());
      Calendar calendar = Calendar.getInstance();
      calendar.setTime(date);
      int month = calendar.get(2);
      dateList.add(1, calendar.getTime());
      if (month >= 0 && month <= 2) {
         calendar.set(2, 0);
      } else if (month >= 3 && month <= 5) {
         calendar.set(2, 3);
      } else if (month >= 6 && month <= 8) {
         calendar.set(2, 6);
      } else {
         calendar.set(2, 9);
      }

      calendar.set(5, 1);
      calendar.set(11, 0);
      calendar.set(12, 0);
      calendar.set(13, 0);
      dateList.add(0, calendar.getTime());
      return dateList;
   }

   public static List<Date> getLastQuarter() {
      List<Date> dateList = new ArrayList();
      Date date = new Date(System.currentTimeMillis());
      Calendar calendar = Calendar.getInstance();
      calendar.setTime(date);
      int month = calendar.get(2);
      if (month >= 0 && month <= 2) {
         calendar.add(1, -1);
         calendar.set(2, 9);
      } else if (month >= 3 && month <= 5) {
         calendar.set(2, 0);
      } else if (month >= 6 && month <= 8) {
         calendar.set(2, 3);
      } else {
         calendar.set(2, 6);
      }

      calendar.set(5, 1);
      calendar.set(11, 0);
      calendar.set(12, 0);
      calendar.set(13, 0);
      dateList.add(0, calendar.getTime());
      calendar.add(2, 3);
      calendar.add(5, -1);
      calendar.set(11, 23);
      calendar.set(12, 59);
      calendar.set(13, 59);
      dateList.add(1, calendar.getTime());
      return dateList;
   }

   public static Date max(Date date1, Date date2) {
      if (date1 == null && date2 == null) {
         return null;
      } else if (date1 == null) {
         return date2;
      } else if (date2 == null) {
         return date1;
      } else {
         return date1.after(date2) ? date1 : date2;
      }
   }

   public static Date ceil(Date date1, Date date2) {
      if (date1 == null && date2 == null) {
         return null;
      } else if (date1 == null) {
         return date2;
      } else if (date2 == null) {
         return date1;
      } else {
         return date1.after(date2) ? date2 : date1;
      }
   }

   public static Date floor(Date date1, Date date2) {
      if (date1 == null && date2 == null) {
         return null;
      } else if (date1 == null) {
         return date2;
      } else if (date2 == null) {
         return date1;
      } else {
         return date1.after(date2) ? date1 : date2;
      }
   }

   public static Date min(Date date1, Date date2) {
      if (date1 == null && date2 == null) {
         return null;
      } else if (date1 == null) {
         return date2;
      } else if (date2 == null) {
         return date1;
      } else {
         return date1.after(date2) ? date2 : date1;
      }
   }

   public static boolean isMaxDayOfDay(Date date1, String precision) {
      if (date1 == null) {
         return false;
      } else {
         Date date2 = getMaxDateOfDay(date1);
         int diffNum = 0;
         if ("HH".equals(precision)) {
            diffNum = getIntervalHour(date1, date2);
         } else if ("mm".equals(precision)) {
            diffNum = getIntervalMinute(date1, date2);
         } else {
            diffNum = getIntervalSecond(date1, date2);
         }

         return diffNum == 0;
      }
   }

   public static boolean isMinDayOfDay(Date date1, String precision) {
      if (date1 == null) {
         return false;
      } else {
         Date date2 = getMinDateOfDay(date1);
         int diffNum = 0;
         if ("HH".equals(precision)) {
            diffNum = getIntervalHour(date1, date2);
         } else if ("mm".equals(precision)) {
            diffNum = getIntervalMinute(date1, date2);
         } else {
            diffNum = getIntervalSecond(date1, date2);
         }

         return diffNum == 0;
      }
   }

   public static boolean isMaxDayOfDay(Date date1) {
      if (date1 == null) {
         return false;
      } else {
         Date date2 = getMaxDateOfDay(date1);
         int secondNum = getIntervalSecond(date1, date2);
         return secondNum == 0;
      }
   }

   public static boolean isMinDayOfDay(Date date1) {
      if (date1 == null) {
         return false;
      } else {
         Date date2 = getMinDateOfDay(date1);
         int secondNum = getIntervalSecond(date1, date2);
         return secondNum == 0;
      }
   }

   public static boolean isMaxDayOfMonth(Date date1) {
      if (date1 == null) {
         return false;
      } else {
         Date date2 = getMaxDateOfMonth(date1);
         int secondNum = getIntervalSecond(date1, date2);
         return secondNum == 0;
      }
   }

   public static boolean isMinDayOfMonth(Date date1) {
      if (date1 == null) {
         return false;
      } else {
         Date date2 = getMinDateOfMonth(date1);
         int secondNum = getIntervalSecond(date1, date2);
         return secondNum == 0;
      }
   }

   public static boolean isTheSameDay(Date startDate, Date endDate) {
      String startDateStr = format(startDate);
      String endDateStr = format(endDate);
      return startDateStr.equals(endDateStr);
   }

   public static Date getLastMaxDay(Date startDate) {
      if (startDate == null) {
         return null;
      } else {
         startDate = addDays(startDate, -1);
         return getMaxDateOfDay(startDate);
      }
   }

   public static String getDifferentStr(String startStr, String endStr) {
      String dateRangeStr = "";
      if (startStr.equals(endStr)) {
         dateRangeStr = startStr;
      } else {
         dateRangeStr = startStr + "-" + endStr;
      }

      return dateRangeStr;
   }

   public static String getNextDay(String date, int num) {
      Date d = parse(date);
      Calendar ca = Calendar.getInstance();
      ca.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
      ca.setTime(d);
      int day = ca.get(5);
      day += num;
      ca.set(5, day);
      return format(ca.getTime());
   }

   public static Date getNextMonthDay(Date startDate) {
      Date monthEndDate = getMaxDateOfMonth(startDate);
      Date nextMonth = addMonths(startDate, 1);
      nextMonth = parse(format(startDate) + " " + "23:59:59");
      if (isTheSameDay(startDate, monthEndDate)) {
         nextMonth = getMaxDateOfMonth(nextMonth);
      }

      return nextMonth;
   }

   public static void main(String[] args) {
   }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值