欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

jdk8对日期的处理工具类

程序员文章站 2022-05-27 11:58:10
...

前言

这两天整理了一下工作中以及平时学习jdk8对日期的处理,因此整理成了一个工具类,为了方便记录学习过程,还是*惯,把代码粘贴出来。

之前还写了一篇日期转换的博客:
Java:String和Date、Timestamp转换以及Calendar对日期的处理以及jdk8对日期的处理
以及昨天写的日期格式和时区的枚举类的博客:
史上最全日期格式枚举类
史上最全 时区枚举类

同时我还参考了如下博客:
1、Java中日期格式化YYYY-DD的坑

2、使用LocalDateTime解析带有微秒,纳秒时间格式yyyy-MM-dd’T’HH:mm:ss.SSSSSSZ

3、java中形如yyyy-MM-dd’T’HH:mm:ss.SSSZ 转化成各种格式的问题

4、Java 8 新特新 工具类 ZonedDateTime

5、使用枚举代替常量,简化工作

工具类:DateUtils

package com.fanhf.javastudy.utils;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

/**
 * @author fanhf
 * @Description jdk8对日期的处理
 * @date 2021-01-12 17:32
 */
public class DateUtils {

    /**
     * @Description  根据给定年月日时间返回LocalDate实例
     * @param  year       年
     * @param  month      月
     * @param  dayOfMonth 日
     * @return LocalDate
     **/
    public static LocalDate selfDefDate(int year, int month, int dayOfMonth){
        if(0 == year || 0 == month || 0 == dayOfMonth){
            return  LocalDate.now();
        }
        return LocalDate.of(year, month, dayOfMonth);
    }

    /**
     * @Description  根据给定年月日时间判断和当前是否相等
     * @param  year       年
     * @param  month      月
     * @param  dayOfMonth 日
     * @return boolean
     **/
    public static boolean dateEquals(int year, int month, int dayOfMonth){
        LocalDate now = LocalDate.now();
        if(0 == year || 0 == month || 0 == dayOfMonth){
            return  false;
        }
        LocalDate defineDate = LocalDate.of(year, month, dayOfMonth);
        if(now.equals(defineDate)) {
            return true;
        }
        return  false;
    }

    /**
     * @Description  根据给定年月日时间判断某年某月某日是否为生日(不论哪一年)
     * @param  year       年
     * @param  month      月
     * @param  dayOfMonth 日
     * @return boolean
     **/
    public static boolean checkBirthday(int year, int month, int dayOfMonth){
        if(0 == year || 0 == month || 0 == dayOfMonth){
            return  false;
        }
        LocalDate now = LocalDate.now();
        LocalDate selfDefine = LocalDate.of(year, month, dayOfMonth);
        MonthDay birthday = MonthDay.of(selfDefine.getMonth(), selfDefine.getDayOfMonth());
        MonthDay currentMonthDay = MonthDay.from(now);
        if(currentMonthDay.equals(birthday)){
            return true;
        }
        return false;
    }

    /**
     * @Description  在当前时分秒的时间上,加上根据给定时分秒
     * @param  hours     时
     * @param  minutes   分
     * @param  seconds   秒
     * @return LocalTime
     **/
    public static  LocalTime plusDates(int hours, int minutes, int seconds){
        LocalTime nowTime = LocalTime.now();
        LocalTime nextTimes = null;
        if(0 == hours && 0 == minutes && 0 == seconds){
            return  nowTime;
        }
        if (0 != hours) {
            nextTimes = nowTime.plusHours(hours);
        }
        if (0 != minutes) {
            nextTimes = nowTime.plusMinutes(minutes);
        }
        if (0 != seconds) {
            nextTimes = nowTime.plusSeconds(seconds);
        }
        if(0 != hours && 0 != minutes){
             nextTimes = nowTime.plusHours(hours).plusMinutes(minutes);
        }
        if(0 != hours && 0 != seconds){
             nextTimes = nowTime.plusHours(hours).plusSeconds(seconds);
        }
        if(0 != minutes && 0 != seconds){
            nextTimes = nowTime.plusMinutes(minutes).plusSeconds(seconds);
        }
        if(0 != hours && 0 != minutes && 0 != seconds){
            nextTimes = nowTime.plusHours(hours).plusMinutes(minutes).plusSeconds(seconds);
        }

        return nextTimes;
    }

    /**
     * @Description  在当前时分秒的时间上,减去根据给定时分秒
     * @param  hours     时
     * @param  minutes   分
     * @param  seconds   秒
     * @return LocalTime
     **/
    public static  LocalTime minusDates(int hours, int minutes, int seconds){
        LocalTime nowTime = LocalTime.now();
        LocalTime previousTimes = null;
        if(0 == hours && 0 == minutes && 0 == seconds){
            return  nowTime;
        }
        if (0 != hours) {
            previousTimes = nowTime.minusHours(hours);
        }
        if (0 != minutes) {
            previousTimes = nowTime.minusMinutes(minutes);
        }
        if (0 != seconds) {
            previousTimes = nowTime.minusSeconds(seconds);
        }
        if(0 != hours && 0 != minutes){
            previousTimes = nowTime.minusHours(hours).minusMinutes(minutes);
        }
        if(0 != hours && 0 != seconds){
            previousTimes = nowTime.minusHours(hours).plusSeconds(seconds);
        }
        if(0 != minutes && 0 != seconds){
            previousTimes = nowTime.plusMinutes(minutes).minusMinutes(seconds);
        }
        if(0 != hours && 0 != minutes && 0 != seconds){
            previousTimes = nowTime.minusHours(hours).plusMinutes(minutes).minusMinutes(seconds);
        }

        return previousTimes;
    }

    /**
     * @Description   在当前年月周日的时间上,加上给定年月周日时间
     * @param  years       年
     * @param  months      月
     * @param  weeks       周
     * @param  days        日
     * @return LocalDate
     **/
    public static LocalDate beforeTimes(int years, int months, int weeks,int days){
        LocalDate today = LocalDate.now();
        LocalDate previousDates = null; //previous
        if(0 == years && 0 == months && 0 == weeks && 0 == days){
            return today;
        }
        if (0 != years) {
            previousDates = today.minusYears(years);
        }
        if (0 != months) {
            previousDates = today.minusMonths(months);
        }
        if (0 != weeks) {
            previousDates = today.minusWeeks(weeks);
        }
        if (0 != days) {
            previousDates = today.minusDays(days);
        }
        if(0 != years && 0 != months){
            previousDates = today.minusYears(years).minusMonths(months);
        }
        if(0 != years && 0 != weeks){
            previousDates = today.minusYears(years).minusWeeks(weeks);
        }
        if(0 != years && 0 != days){
            previousDates = today.minusYears(years).minusDays(days);
        }
        if( 0 != months && 0 != weeks){
            previousDates = today.minusMonths(months).minusWeeks(weeks);
        }
        if( 0 != months && 0 != days){
            previousDates = today.minusMonths(months).minusDays(days);
        }
        if(0 != weeks && 0 != days){
            previousDates = today.minusWeeks(weeks).minusDays(days);
        }
        if(0 != years && 0 != months && 0 != weeks){
            previousDates = today.minusYears(years).minusMonths(months).minusWeeks(weeks);
        }
        if(0 != years && 0 != months && 0 != days){
            previousDates = today.minusYears(years).minusMonths(months).minusDays(days);
        }
        if( 0 != months && 0 != weeks && 0 != days){
            previousDates = today.minusMonths(months).minusWeeks(weeks).minusDays(days);
        }
        if(0 != years && 0 != months && 0 != weeks && 0 != days){
            previousDates = today.minusYears(years).minusMonths(months).minusWeeks(weeks).minusDays(days);
        }
        return previousDates;
    }

    /**
     * @Description   在当前年月周日的时间上,加上给定年月周日时间
     * @param  years       年
     * @param  months      月
     * @param  weeks       周
     * @param  days        日
     * @return LocalDate
     **/
    public static LocalDate afterTimes(int years, int months, int weeks,int days){
        LocalDate today = LocalDate.now();
        LocalDate nextDate = null;
        if(0 == years && 0 == months && 0 == weeks && 0 == days){
            return today;
        }
        if (0 != years) {
            nextDate = today.plusYears(years);
        }
        if (0 != months) {
            nextDate = today.plusMonths(months);
        }
        if (0 != weeks) {
            nextDate = today.plusWeeks(weeks);
        }
        if (0 != days) {
            nextDate = today.plusDays(days);
        }
        if(0 != years && 0 != months){
            nextDate = today.plusYears(years).plusMonths(months);
        }
        if(0 != years && 0 != weeks){
            nextDate = today.plusYears(years).plusWeeks(weeks);
        }
        if(0 != years && 0 != days){
            nextDate = today.plusYears(years).plusDays(days);
        }
        if( 0 != months && 0 != weeks){
            nextDate = today.plusMonths(months).plusWeeks(weeks);
        }
        if( 0 != months && 0 != days){
            nextDate = today.plusMonths(months).plusDays(days);
        }
        if(0 != weeks && 0 != days){
            nextDate = today.plusWeeks(weeks).plusDays(days);
        }
        if(0 != years && 0 != months && 0 != weeks){
            nextDate = today.plusYears(years).plusMonths(months).plusWeeks(weeks);
        }
        if(0 != years && 0 != months && 0 != days){
            nextDate = today.plusYears(years).plusMonths(months).plusDays(days);
        }
        if( 0 != months && 0 != weeks && 0 != days){
            nextDate = today.plusMonths(months).plusWeeks(weeks).plusDays(days);
        }
        if(0 != years && 0 != months && 0 != weeks && 0 != days){
            nextDate = today.plusYears(years).plusMonths(months).plusWeeks(weeks).plusDays(days);
        }
        return nextDate;
    }

    /**
     * @Description    在当前年份上,加上给定年数
     * @param  pulus1  年数
     * @return LocalDate
     **/
    public static LocalDate yearPlus1(int pulus1){
        LocalDate today = LocalDate.now();
        if (0 == pulus1) {
            return today;
        }
        LocalDate handleYear = null;
        if (0 != pulus1) {
           handleYear = today.plus(pulus1, ChronoUnit.YEARS);
        }
        return  handleYear;
    }
    /**
     * @Description   在当前年份上,减去给定年数
     * @param  minus1 年数
     * @return LocalDate
     **/
    public static LocalDate yearMinus1(int minus1){
        LocalDate today = LocalDate.now();
        if (0 == minus1) {
            return today;
        }
        LocalDate handleYear = null;
        if (0 != minus1) {
            handleYear = today.minus(minus1, ChronoUnit.YEARS);
        }
        return  handleYear;
    }

    /**
     * @Description   返回UTC时区的当前年月日时分秒毫秒的毫秒值
     * @return long
     **/
    public static long clockMillis(){
        return Clock.systemUTC().millis();
    }

    /**
     * @Description   返回默认时区的当前年月日时分秒毫秒的时间戳
     * @return long
     **/
    public static long clockZoneMillis(){
        return  Clock.systemDefaultZone().millis();
    }

    /**
     * @Description   返回默认时区的当前年月日时分秒毫秒的时间戳
     * @return  long 当前时间的时分秒毫秒
     **/
    public  static long currentTimeStamp(){
        Instant now = Instant.now();
        return now.toEpochMilli();
    }

    /**
     * @Description 在当前时间点的基础上减去给定天数的时间
     * @param  beforeDays  当前日期的前多少天
     * @param format       日期格式
     * case 7:最近一周(前6天)
     * case 14:最近两周(前13天)
     * case 30:最近一个月(前29天)
     * case 90:最近三个月(前89天)
     * @return String
     */
    public static String getTimeBeforeDays(Integer beforeDays,String format){

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(format);
        LocalDateTime localDateTime =  LocalDateTime.now();
        if(null == beforeDays) {
            return localDateTime.format(dateTimeFormatter);
        }
        LocalDateTime minus  = null;
        switch (beforeDays.intValue()){
            case 7:
                minus = localDateTime.minusDays(6L);
                break;
            case 14:
                minus = localDateTime.minusDays(13L);
                break;
            case 30:
                minus = localDateTime.minusDays(29L);
                break;
            case 90:
                minus = localDateTime.minusDays(89L);
                break;
            default:
                minus = localDateTime;
        }
        String time = minus.format(dateTimeFormatter) ;
        return time;
    }

    /**
     * @Description   返回默认时区的当前年月日时分秒毫秒的时间戳
     * @param time  自定义时间
     * @param format 时间格式
     * @return  long 当前时间的时分秒毫秒
     *
     * @apiNote  time的格式和format的格式必须一致,否则就会异常
     **/
    public  static long getSeconds(String time,String format){
        DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
        LocalDateTime   localDateTime = LocalDateTime.parse(time, df);
        ZoneId zone = ZoneId.systemDefault();
        Instant instant = localDateTime.atZone(zone).toInstant();
        return instant.toEpochMilli()/1000;
    }

    /**
     * @Description   判断根据给定的年月日在当前年月日之前还是之后
     * @param year       年
     * @param month      月
     * @param dayOfMonth 日
     * @return boolean
     **/

    public static boolean dateAfter(int year, int month, int dayOfMonth){
        LocalDate today = LocalDate.now();
        LocalDate compareDate = LocalDate.of(year, month, dayOfMonth);
        if(compareDate.isAfter(today)){
            return true;
        }
        return  false;
    }

    /**
     * @Description   将当前时间所在时区转为指定时区的时间
     * @param   zoneId  时区名称(从ZoneEnum枚举类中获取)
     * @return ZonedDateTime
     **/

    public static ZonedDateTime zoneTransform(String zoneId){
        ZoneId zone = ZoneId.of(zoneId);
        LocalDateTime now = LocalDateTime.now();
        return ZonedDateTime.of(now, zone);
    }

    /**
     * @Description  获取当前月的最后一天
     * @return  LocalDate
     **/

    public  static LocalDate EndOfMonth(){
        YearMonth currenYearMonth = YearMonth.now();
        return  currenYearMonth.atEndOfMonth();
    }

    /**
     * @Description   根据给定年份的当前月的天数
     * @return   int
     **/

    public  static int lengthOfMonth(){
        YearMonth currenYearMonth = YearMonth.now();
        return  currenYearMonth.lengthOfMonth();
    }

    /**
     * @Description   判断当前年份是平年还是闰年
     * @return  true是闰年,false是平年
     **/

    public static boolean leapYear(){
        LocalDate now = LocalDate.now();
        if(now.isLeapYear()){
            return true;
        }
        return false;
    }


    /**
     * @Description   根据给定年月日,算出和当前年月日相差的时间
     * @param year       年
     * @param month      月
     * @param dayOfMonth 日
     * @return  Period 相差时间,可以获取到相差的年月日
     **/
    public static Period calculateGapInDaysOrMonth(int year, int month, int dayOfMonth) {
        LocalDate now = LocalDate.now();
        LocalDate selfDefine = LocalDate.of(year, month, dayOfMonth);
        return Period.between(now, selfDefine);
    }

    /**
     * @Description   根据给定年月日,算出和当前时分秒相差的时间
     * @param time      时间
     * @param format 从枚举类中:DateFomateEnum获取
     * @return  Duration 相差时间,可以获取到相差的时分秒
     * @apiNote  time的格式和format的格式必须一致,否则就会异常
     **/
    public  static Duration calculateDurationInTimes(String time,String format){
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
        LocalDateTime  selfDefineTime = LocalDateTime.parse(time, df);
        Duration duration = Duration.between(selfDefineTime,now);
        return duration;
    }

    /**
     * @Description   将给定日期格式化为BASIC_ISO_DATE的日期格式
     * @param  date 给定日期
     * @return  LocalDate 格式化后的日期 ,date 的格式必须为 YYYYMMDD,其他的格式都无法解析
     **/
    public static LocalDate fomateDate(String date){
       return LocalDate.parse(date, DateTimeFormatter.BASIC_ISO_DATE);
    }

    /**
     * @Description   将当前时间格式化为指定的时间格式后转成string
     * @param format 从枚举类中:DateFomateEnum获取
     * @return String 自定义格式化String类型日期
     **/
    public static String  localDate2String(String format){
        LocalDateTime dateTime = LocalDateTime.now();
        String formattedTime = null;
        if (format.toLowerCase().indexOf("z") != -1) {
            ZonedDateTime zdt2 = ZonedDateTime.of(dateTime, ZoneId.systemDefault());
            formattedTime = DateTimeFormatter.ofPattern(format).format(zdt2);
        }else {
            formattedTime = dateTime.format(DateTimeFormatter.ofPattern(format));
        }
        return formattedTime;
    }
    /**
     * @Description   将给定时间格式化为指定的时间格式后转成string
     * @param format 从枚举类中:DateFomateEnum获取
     * @return String 自定义格式化String类型日期
     * @apiNote  time的格式和format的格式必须一致,否则就会异常
     **/
    public static String  selfDate2String(String time,String format){
        DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
        LocalDateTime  selfDefineTime = LocalDateTime.parse(time, df);
        String formattedTime = null;
        if (format.toLowerCase().indexOf("z") != -1) {
            ZonedDateTime zdt2 = ZonedDateTime.of(selfDefineTime, ZoneId.systemDefault());
            formattedTime = DateTimeFormatter.ofPattern(format).format(zdt2);
        }else {
            formattedTime = selfDefineTime.format(DateTimeFormatter.ofPattern(format));
        }
        return formattedTime;
    }
    /**
     * @Description   将给定时间格式化为指定的时间格式
     * @return  LocalDate 自定义格式日期类型
     * @param format 从枚举类中:DateFomateEnum获取
     * @apiNote  time的格式和format的格式必须一致,否则就会异常
     **/

    public static LocalDate string2LocalDate(String date,String format){
        LocalDateTime dateTime = LocalDateTime.now();
        LocalDate formattedTime = null;

        if (format.toLowerCase().indexOf("z") != -1) {
            LocalDateTime ldt = LocalDateTime.parse(date, DateTimeFormatter.ofPattern(format));
            formattedTime = ZonedDateTime.of(ldt, ZoneId.systemDefault()).toLocalDate();
        }else {
            formattedTime = LocalDate.parse(date, DateTimeFormatter.ofPattern(format));
        }
        return formattedTime;
    }
}

另外,我还写了个单元测试类来测试自己写的这些代码:

日期测试类:DateUtilsTest

package com.fanhf.javastudy.utils;

import com.fanhf.javastudy.enumTest.DateFomateEnum;
import com.fanhf.javastudy.enumTest.ZoneEnum;
import org.junit.Assume;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class DateUtilsTest {

    @Test
    void selfDefDate() {
        System.out.println(DateUtils.selfDefDate(2021,1,1).getYear());
    }

    @Test
    void dateEquals() {
        System.out.println(DateUtils.dateEquals(2021,0,0));
    }

    @Test
    void checkBirthday() {
        System.out.println(DateUtils.checkBirthday(0,1,13));
    }

    @Test
    void plusDates() {
        System.out.println(DateUtils.plusDates(0,0,0));
        System.out.println(DateUtils.plusDates(5,0,0));
        System.out.println(DateUtils.plusDates(0,10,0));
        System.out.println(DateUtils.plusDates(0,0,10));
        System.out.println(DateUtils.plusDates(5,10,0));
        System.out.println(DateUtils.plusDates(0,10,10));
        System.out.println(DateUtils.plusDates(5,0,10));
        System.out.println(DateUtils.plusDates(5,10,10));
    }

    @Test
    void minusDates(){
        System.out.println(DateUtils.minusDates(0,0,0));
        System.out.println(DateUtils.minusDates(5,0,0));
        System.out.println(DateUtils.minusDates(0,10,0));
        System.out.println(DateUtils.minusDates(0,0,10));
        System.out.println(DateUtils.minusDates(5,10,0));
        System.out.println(DateUtils.minusDates(0,10,10));
        System.out.println(DateUtils.minusDates(5,0,10));
        System.out.println(DateUtils.minusDates(5,10,10));
    }

    @Test
    void afterTimes() {
        System.out.println(DateUtils.afterTimes(0,0,0,0));
        System.out.println(DateUtils.afterTimes(1,0,0,0));
        System.out.println(DateUtils.afterTimes(0,1,0,0));
        System.out.println(DateUtils.afterTimes(0,0,1,0));
        System.out.println(DateUtils.afterTimes(0,0,0,1));
        System.out.println(DateUtils.afterTimes(1,1,0,0));
        System.out.println(DateUtils.afterTimes(1,0,1,0));
        System.out.println(DateUtils.afterTimes(1,0,0,1));
        System.out.println(DateUtils.afterTimes(0,1,1,0));
        System.out.println(DateUtils.afterTimes(0,1,0,1));
        System.out.println(DateUtils.afterTimes(0,0,1,1));
        System.out.println(DateUtils.afterTimes(1,1,1,0));
        System.out.println(DateUtils.afterTimes(0,1,1,1));
        System.out.println(DateUtils.afterTimes(1,1,0,1));
        System.out.println(DateUtils.afterTimes(1,1,1,1));
    }

    @Test
    void beforeTimes(){
        System.out.println(DateUtils.beforeTimes(0,0,0,0));
        System.out.println(DateUtils.beforeTimes(1,0,0,0));
        System.out.println(DateUtils.beforeTimes(0,1,0,0));
        System.out.println(DateUtils.beforeTimes(0,0,1,0));
        System.out.println(DateUtils.beforeTimes(0,0,0,1));
        System.out.println(DateUtils.beforeTimes(1,1,0,0));
        System.out.println(DateUtils.beforeTimes(1,0,1,0));
        System.out.println(DateUtils.beforeTimes(1,0,0,1));
        System.out.println(DateUtils.beforeTimes(0,1,1,0));
        System.out.println(DateUtils.beforeTimes(0,1,0,1));
        System.out.println(DateUtils.beforeTimes(0,0,1,1));
        System.out.println(DateUtils.beforeTimes(1,1,1,0));
        System.out.println(DateUtils.beforeTimes(0,1,1,1));
        System.out.println(DateUtils.beforeTimes(1,1,0,1));
        System.out.println(DateUtils.beforeTimes(1,1,1,1));
    }

    @Test
    void yearPlus1() {
        System.out.println(DateUtils.yearPlus1(0));
    }

    @Test
    void yearMinus1() {
        System.out.println(DateUtils.yearMinus1(3));
    }

    @Test
    void clockMillis() {
        System.out.println(DateUtils.clockMillis());
    }

    @Test
    void clockZoneMillis() {
        System.out.println(DateUtils.clockZoneMillis());
    }

    @Test
    void currentTimeStamp() {
        System.out.println(DateUtils.currentTimeStamp());
    }

    @Test
    void getTimeBeforeDays(){
        /**
         * case 7:最近一周(前6天)
         * case 14:最近两周(前13天)
         * case 30:最近一个月(前29天)
         * case 90:最近三个月(前89天)
         */
        System.out.println(DateUtils.getTimeBeforeDays(4,DateFomateEnum.DATE_FORMAT_SIMPLE_YYYY_MM_DD_HH_MM_SS.getName()));
        System.out.println(DateUtils.getTimeBeforeDays(7,DateFomateEnum.DATE_FORMAT_SIMPLE_YYYY_MM_DD_HH_MM_SS.getName()));
        System.out.println(DateUtils.getTimeBeforeDays(14,DateFomateEnum.DATE_FORMAT_SIMPLE_YYYY_MM_DD_HH_MM_SS.getName()));
        System.out.println(DateUtils.getTimeBeforeDays(30,DateFomateEnum.DATE_FORMAT_SIMPLE_YYYY_MM_DD_HH_MM_SS.getName()));
        System.out.println(DateUtils.getTimeBeforeDays(90,DateFomateEnum.DATE_FORMAT_SIMPLE_YYYY_MM_DD_HH_MM_SS.getName()));
    }

    @Test
    void getSeconds(){
        System.out.println(DateUtils.getSeconds("2020-01-01 00:00:00",DateFomateEnum.DATE_FORMAT_SIMPLE_YYYY_MM_DD_HH_MM_SS.getName()));
    }

    @Test
    void dateAfter() {
        System.out.println(DateUtils.dateAfter(2021,1,14));
    }

    @Test
    void zoneTransform() {
        System.out.println(DateUtils.zoneTransform(ZoneEnum.CTT.getName()));
    }

    @Test
    void creditCardExpired() {
        System.out.println(DateUtils.EndOfMonth());
    }

    @Test
    void lengthOfMonth() {
        System.out.println(DateUtils.lengthOfMonth());
    }

    @Test
    void leapYear() {
        System.out.println(DateUtils.leapYear());
    }

    @Test
    void calculateGapInDaysOrMonth() {
        System.out.println(DateUtils.calculateGapInDaysOrMonth(2020,1,1));
        System.out.println(DateUtils.calculateGapInDaysOrMonth(2020,1,1).getDays());
        System.out.println(DateUtils.calculateGapInDaysOrMonth(2020,1,1).getYears());
        System.out.println(DateUtils.calculateGapInDaysOrMonth(2020,1,1).getMonths());
        System.out.println(DateUtils.calculateGapInDaysOrMonth(2020,1,1).minusDays(10));
        System.out.println(DateUtils.calculateGapInDaysOrMonth(2020,1,1).minusDays(10).getDays());
        System.out.println(DateUtils.calculateGapInDaysOrMonth(2020,1,1).plusMonths(1));
        System.out.println(DateUtils.calculateGapInDaysOrMonth(2020,1,1).plusMonths(1).getMonths());
        System.out.println(DateUtils.calculateGapInDaysOrMonth(2020,1,1).withDays(5));
    }
    @Test
    void  calculateDurationInTimes(){
        System.out.println(DateUtils.calculateDurationInTimes(
                "2020-01-01 00:00:00",DateFomateEnum.DATE_FORMAT_SIMPLE_YYYY_MM_DD_HH_MM_SS.getName()).minusDays(10).getSeconds());
        System.out.println(DateUtils.calculateDurationInTimes(
                "2020/01/01 00:00:00",DateFomateEnum.DATE_FORMAT_SLASH_YYYY_MM_DD_HH_MM_SS.getName()).getSeconds());
        System.out.println(DateUtils.calculateDurationInTimes(
                "20200101 00:00:00",DateFomateEnum.DATE_FORMAT_SIMPLE_YYYYMMDD_HHMMSS.getName()).toMinutes());
        System.out.println(DateUtils.calculateDurationInTimes(
                "20200101 00:00:00",DateFomateEnum.DATE_FORMAT_SIMPLE_YYYYMMDD_HHMMSS.getName()).toDays());
        System.out.println(DateUtils.calculateDurationInTimes(
                "2020-01-01 00:00:00",DateFomateEnum.DATE_FORMAT_SIMPLE_YYYY_MM_DD_HH_MM_SS.getName()).toHours());
    }
    @Test
    void fomateDate() {
        System.out.println(DateUtils.fomateDate("20210101"));
    }

    @Test
    void localDate2String() {
        System.out.println(DateUtils.localDate2String(DateFomateEnum.DATE_FORMAT_RFC_1123_3_YEAR.getName()));
        System.out.println(DateUtils.localDate2String(DateFomateEnum.DATE_FORMAT_RFC_1123_MONTH_YEAR.getName()));
        System.out.println(DateUtils.localDate2String(DateFomateEnum.DATE_FORMAT_RFC_1123_MONTH_YEAR_NO_SECOND.getName()));
        System.out.println(DateUtils.localDate2String(DateFomateEnum.DATE_FORMAT_RFC_1123_VARIANT.getName()));
        System.out.println("-------------------------------------------------");

        System.out.println(DateUtils.localDate2String(DateFomateEnum.DATE_FORMAT_RFC_1123_3_TIMEZONE.getName()));
        System.out.println(DateUtils.localDate2String(DateFomateEnum.DATE_FORMAT_RFC_1123_TIMEZONE.getName()));
        System.out.println(DateUtils.localDate2String(DateFomateEnum.DATE_FORMAT_RFC_1123_NO_SECOND_TIMEZONE.getName()));
        System.out.println(DateUtils.localDate2String(DateFomateEnum.DATE_FORMAT_RFC_1123_VARIANT_TIMEZONE.getName()));
        System.out.println("-------------------------------------------------");

        System.out.println(DateUtils.localDate2String(DateFomateEnum.DATE_FORMAT_RFC_1123_NO_WEEKDAY_TIMEZONE_NO_SECOND.getName()));
        System.out.println(DateUtils.localDate2String(DateFomateEnum.DATE_FORMAT_RFC_1123_NO_WEEKDAY_TIMEZONE_4_YEAR.getName()));
        System.out.println(DateUtils.localDate2String(DateFomateEnum.DATE_FORMAT_RFC_1123_NO_WEEKDAY_SECOND_4_YEAR_HOUR.getName()));
        System.out.println("-------------------------------------------------");

        System.out.println(DateUtils.localDate2String(DateFomateEnum.DATE_FORMAT_RFC_822_2_YEAR_ZONE_OFFSET.getName()));
        System.out.println(DateUtils.localDate2String(DateFomateEnum.DATE_FORMAT_RFC_822_4_YEAR_ZONE_OFFSET.getName()));
        System.out.println(DateUtils.localDate2String(DateFomateEnum.DATE_FORMAT_RFC_822_3_4_YEAR_ZONE_OFFSET.getName()));
        System.out.println(DateUtils.localDate2String(DateFomateEnum.DATE_FORMAT_RFC_822_4_VARIANT_ZONE_OFFSET.getName()));
        System.out.println("-------------------------------------------------");

        System.out.println(DateUtils.localDate2String(DateFomateEnum.DATE_FORMAT_SIMPLE_2.getName()));
        System.out.println("-------------------------------------------------");

        System.out.println(DateUtils.localDate2String(DateFomateEnum.DATE_FORMAT_ISO_8601_MODE_0.getName()));
        System.out.println(DateUtils.localDate2String(DateFomateEnum.DATE_FORMAT_ISO_8601_MODE_1.getName()));
        System.out.println(DateUtils.localDate2String(DateFomateEnum.DATE_FORMAT_ISO_8601_MODE_2.getName()));
        System.out.println(DateUtils.localDate2String(DateFomateEnum.DATE_FORMAT_ISO_8601_MODE_3.getName()));
        System.out.println(DateUtils.localDate2String(DateFomateEnum.DATE_FORMAT_ISO_8601_MODE_4.getName()));
        System.out.println(DateUtils.localDate2String(DateFomateEnum.DATE_FORMAT_ISO_8601_MODE_5.getName()));
        System.out.println(DateUtils.localDate2String(DateFomateEnum.DATE_FORMAT_ISO_8601_MODE_6.getName()));
        System.out.println(DateUtils.localDate2String(DateFomateEnum.DATE_FORMAT_ISO_8601_MODE_7.getName()));
        System.out.println(DateUtils.localDate2String(DateFomateEnum.DATE_FORMAT_ISO_8601_MODE_8.getName()));
        System.out.println(DateUtils.localDate2String(DateFomateEnum.DATE_FORMAT_ISO_8601_MODE_9.getName()));
        System.out.println("-------------------------------------------------");

        System.out.println(DateUtils.localDate2String(DateFomateEnum.DATE_FORMAT_SIMPLE_YYYY_MM_DD_HH_MM_SS.getName()));
        System.out.println(DateUtils.localDate2String(DateFomateEnum.DATE_FORMAT_SLASH_YYYY_MM_DD_HH_MM_SS.getName()));
        System.out.println(DateUtils.localDate2String(DateFomateEnum.DATE_FORMAT_SIMPLE_YYYYMMDD_HHMMSS.getName()));
    }
    @Test
    void selfDate2String(){
        System.out.println(DateUtils.selfDate2String("2020-01-01 00:00:00",DateFomateEnum.DATE_FORMAT_SIMPLE_YYYY_MM_DD_HH_MM_SS.getName()));
    }

    @Test
    void string2LocalDate() {
        System.out.println(DateUtils.string2LocalDate("20200101 00:00:00",DateFomateEnum.DATE_FORMAT_SIMPLE_YYYYMMDD_HHMMSS.getName()));
    }
}

欢迎指正和补充!!!
--------------------------与君共勉-------------------------