Date Time Java API Series 18 ----- Jdk8 new date and time API class java.time package, java date calculations 5, other common date calculation, calculation of the week, leap year calculation

  By LocalDate Java source code analysis API Date Time Series 8 ----- Jdk8 new date and time API class java.time package can be seen in, java8 Method particularly rich, some common calculations such as calculations week, leap year calculation, and so on.

Here is the application code:

    / ** 
     * Gets day 1-7, Monday to Sunday 
     * @param DATE 
     * @return 
     * / 
    public  static  int the getDayOfWeek (a Date DATE) {
         return DateTimeConverterUtil.toLocalDateTime (DATE) .getDayOfWeek () the getValue ();. 
    } 
    
    / ** 
     * get the day 1-7, Monday to Sunday 
     * @param the LocalDateTime 
     * @return 
     * / 
    public  static  int getDayOfWeek (the LocalDateTime the LocalDateTime) { 
        Objects.requireNonNull (the LocalDateTime, "the LocalDateTime" );
         returnlocalDateTime.getDayOfWeek () the getValue ();. 
    } 
    
    / ** 
     * Gets day 1-7, Monday to Sunday 
     * @param Instant 
     * @return 
     * / 
    public  static  int the getDayOfWeek (the Instant Instant) {
         return DateTimeConverterUtil.toLocalDateTime (Instant ) .getDayOfWeek () getValue ();. 
    } 
    
    / ** 
     * Gets the current day of the week last day of the month 
     * @param localDate 
     * @return 
     * / 
    public  static LocalDate lastDayOfMonth (LocalDate localDate) { 
        Objects.requireNonNull (localDate, "localDate" );
        return localDate.with (TemporalAdjusters.lastDayOfMonth ()); 
    } 
    
    / ** 
     * Gets the value of the week of the current month on the last day 
     * @param LocalDateTime 
     * @return 
     * / 
    public  static the LocalDateTime lastDayOfMonth (the LocalDateTime LocalDateTime) { 
        Objects.requireNonNull (LocalDateTime, " the LocalDateTime " );
         return localDateTime.with (TemporalAdjusters.lastDayOfMonth ()); 
    }     
    
    / ** 
     * Gets the current day of the week last day of the month 
     * @param DATE 
     * @return 
     * / 
    public  static Date lastDayOfMonth(Date date){
        return DateTimeConverterUtil.toDate(DateTimeConverterUtil.toLocalDate(date).with(TemporalAdjusters.lastDayOfMonth()));
    }    
    
    /**
     * 判断是否闰年
     * @param localDate
     * @return
     */
    public static boolean isLeapYear(LocalDate localDate){
        Objects.requireNonNull(localDate, "localDate");
        return localDate.isLeapYear();
    }
    
    /**
     * 判断是否闰年
     * @param localDateTime
     * @return
     */
    public static boolean isLeapYear(LocalDateTime localDateTime){
        Objects.requireNonNull(localDateTime, "localDateTime");
        return localDateTime.toLocalDate().isLeapYear();
    }
    
    /**
     * 判断是否闰年
     * @param date
     * @return
     */
    public static boolean isLeapYear(Date date){
        return DateTimeConverterUtil.toLocalDateTime(date).toLocalDate().isLeapYear();
    }
    
    /**
     * 获取月的天数
     * @param localDate
     * @return
     */
    public static int lengthOfMonth(LocalDate localDate){
        Objects.requireNonNull(localDate, "localDate");
        return localDate.lengthOfMonth();
    }
    
    /**
     * 获取月的天数
     * @param localDateTime
     * @return
     */
    public static int lengthOfMonth(LocalDateTime localDateTime){
        Objects.requireNonNull(localDateTime, "localDateTime");
        return localDateTime.toLocalDate().lengthOfMonth();
    }
    
    /**
     * 获取月的天数
     * @param date
     * @return
     */
    public static int lengthOfMonth(Date date){
        return DateTimeConverterUtil.toLocalDateTime(date).toLocalDate().lengthOfMonth();
    }
    
    /**
     *  获取年的天数
     * @param localDate
     * @return
     */
    public static int lengthOfYear(LocalDate localDate){
        Objects.requireNonNull(localDate, "localDate");
        return localDate.lengthOfYear();
    }
    
    /**
     * 获取年的天数
     * @param localDateTime
     * @return
     */
    public static int lengthOfYear(LocalDateTime localDateTime){
        Objects.requireNonNull(localDateTime, "localDateTime");
        return localDateTime.toLocalDate().lengthOfYear();
    }
    
    /**
     * 获取年的天数
     * @param date
     * @return
     */
    public static int lengthOfYear(Date date){
        return DateTimeConverterUtil.toLocalDateTime(date).toLocalDate().lengthOfYear();
    }    
    
    /**
     * 下一个星期几
     * @param localDate
     * @param dayOfWeek
     * @return
     */
    public static LocalDate next(LocalDate localDate, DayOfWeek dayOfWeek){
        Objects.requireNonNull(localDate, "localDate");
        return localDate.with(TemporalAdjusters.next(dayOfWeek));
    }
    
    /**
     * 下一个星期几
     * @param localDateTime
     * @param dayOfWeek
     * @return
     */
    public static LocalDateTime next(LocalDateTime localDateTime, DayOfWeek dayOfWeek){
        returnlocalDateTime.with (TemporalAdjusters.next (dayOfWeek)); 
    } 

    / ** 
     * next week 
     * @param DATE 
     * @param dayOfWeek 
     * @return 
     * / 
    public  static a Date Next (a Date DATE, DayOfWeek dayOfWeek) {
         return DateTimeConverterUtil.toDate (DateTimeConverterUtil.toLocalDate (DATE) .with (TemporalAdjusters.next (dayOfWeek))); 
    } 
    
    
    / ** 
     * a week 
     * @param localDate 
     * @param dayOfWeek 
     * @return 
     * / 
    public  staticPrevious the LocalDate (the LocalDate localDate, DayOfWeek dayOfWeek) { 
        Objects.requireNonNull (localDate, "localDate" );
         return localDate.with (TemporalAdjusters.previous (dayOfWeek)); 
    } 
    
    / ** 
     * a few one week 
     * @param LocalDateTime 
     * @param dayOfWeek 
     * @return 
     * / 
    public  static the LocalDateTime Previous (LocalDateTime the LocalDateTime, dayOfWeek dayOfWeek) {
         return localDateTime.with (TemporalAdjusters.previous (dayOfWeek)); 
    } 

    / ** 
     a * a week 
     * @param DATE
     * @Param dayOfWeek 
     * @return 
     * / 
    public  static a Date Previous (a Date DATE, DayOfWeek dayOfWeek) {
         return DateTimeConverterUtil.toDate (DateTimeConverterUtil.toLocalDate (DATE) .with (TemporalAdjusters.previous (dayOfWeek))); 
    } 
    
    / ** 
     * eligible the next working day 
     * @param localDate 
     * @return 
     * / 
    public  static the localDate nextWorkDay (the localDate localDate) { 
        Objects.requireNonNull (localDate, "localDate" );
         return localDate.with(TemporalAdjusterExtension.nextWorkDay());
    } 
    
    / ** 
     * eligible for the next working day 
     * @param LocalDateTime 
     * @return 
     * / 
    public  static the LocalDateTime nextWorkDay (the LocalDateTime LocalDateTime) { 
        Objects.requireNonNull (LocalDateTime, "LocalDateTime" );
         return localDateTime.with (TemporalAdjusterExtension.nextWorkDay ()) ; 
    } 
    
    / ** 
     * eligible for the next working day 
     * @param DATE 
     * @return 
     * / 
    public  static a Date nextWorkDay (a Date DATE) {
         return DateTimeConverterUtil.toDate(DateTimeConverterUtil.toLocalDate(date).with(TemporalAdjusterExtension.nextWorkDay()));
    }

 

Test code:

 

    / ** 
     * other common computing a Date 
     * / 
    @Test 
    public  void dateCalculatorOtherTest () { 
        a Date DATE = new new a Date (); 
        System.out.println (DATE); 
        System.out.println (DateTimeConverterUtil.toLocalDateTime (DATE)); 
        / / get the week 
        System.out.println (DateTimeCalculatorUtil.getDayOfWeek (DATE));
         // Gets the current day of the week last day of the month 
        System.out.println (DateTimeCalculatorUtil.lastDayOfMonth (DATE));
         // determine whether a leap year 
        System.out .println (DateTimeCalculatorUtil.isLeapYear (DATE));
         // get the number of days of the month
        System.out.println (DateTimeCalculatorUtil.lengthOfMonth (DATE));
         // get the number of days of the month 
        System.out.println (DateTimeCalculatorUtil.lengthOfYear (DATE));
         // next Monday 
        System.out.println (DateTimeCalculatorUtil.next ( DATE, DayOfWeek.MONDAY));
         // Monday on 
        System.out.println (DateTimeCalculatorUtil.previous (DATE, DayOfWeek.MONDAY));
         // won the next working day 
        System.out.println (DateTimeCalculatorUtil.nextWorkDay (date )); 
    } 
    
    / ** 
     * the LocalDateTime other common calculation 
     * / 
    @Test 
    public  void dateCalculatorOtherTest2 () { 
        the LocalDateTime LDT =LocalDateTime.now (); 
        System.out.println (LDT); 
        
        // get the week 
        System.out.println (DateTimeCalculatorUtil.getDayOfWeek (LDT));
         // get the week last day of the current month 
        System.out.println ( DateTimeCalculatorUtil.lastDayOfMonth (LDT));
         // determine whether a leap year 
        System.out.println (DateTimeCalculatorUtil.isLeapYear (LDT));
         // get the number of days of the month 
        System.out.println (DateTimeCalculatorUtil.lengthOfMonth (LDT));
         // Gets the number of days the month 
        System.out.println (DateTimeCalculatorUtil.lengthOfYear (LDT));
         // next Monday 
        System.out.println (DateTimeCalculatorUtil.next (ldt, DayOfWeek.MONDAY)
         );// a last Monday 
        System.out.println (DateTimeCalculatorUtil.previous (LDT, DayOfWeek.MONDAY));
         // eligible for the next working day 
        System.out.println (DateTimeCalculatorUtil.nextWorkDay (LDT)); 
    }

 

Test Results:

Fri Feb 07 18:34:40 CST 2020
2020-02-07T18:34:40.887
5
Sat Feb 29 00:00:00 CST 2020
true
29
366
Mon Feb 10 00:00:00 CST 2020
Mon Feb 03 00:00:00 CST 2020
Mon Feb 10 00:00:00 CST 2020




2020-02-07T18:34:56.421
5
2020-02-29T18:34:56.421
true
29
366
2020-02-10T18:34:56.421
2020-02-03T18:34:56.421
2020-02-10T18:34:56.421

 

Source address: https://github.com/xkzhangsan/xk-time

Guess you like

Origin www.cnblogs.com/xkzhangsanx/p/12274065.html