Java日期时间API系列18-----Jdk8中java.time包中的新的日期时间API类,java日期计算5,其他常用日期计算,星期计算,闰年计算等

笑着哭i 提交于 2020-02-07 19:42:13

  通过Java日期时间API系列8-----Jdk8中java.time包中的新的日期时间API类的LocalDate源码分析中可以看出,java8中的方法特别丰富,一些常用的计算如星期计算,闰年计算等等。

下面是应用代码:

    /**
     * 获取星期值 1-7,星期一到星期日
     * @param date
     * @return
     */
    public static int getDayOfWeek(Date date){
        return DateTimeConverterUtil.toLocalDateTime(date).getDayOfWeek().getValue();
    }
    
    /**
     * 获取星期值 1-7,星期一到星期日
     * @param localDateTime
     * @return
     */
    public static int getDayOfWeek(LocalDateTime localDateTime){
        Objects.requireNonNull(localDateTime, "localDateTime");
        return localDateTime.getDayOfWeek().getValue();
    }
    
    /**
     * 获取星期值 1-7,星期一到星期日
     * @param instant
     * @return
     */
    public static int getDayOfWeek(Instant instant){
        return DateTimeConverterUtil.toLocalDateTime(instant).getDayOfWeek().getValue();
    }
    
    /**
     * 获取星期值当前月的最后一天
     * @param localDate
     * @return
     */
    public static LocalDate lastDayOfMonth(LocalDate localDate){
        Objects.requireNonNull(localDate, "localDate");
        return localDate.with(TemporalAdjusters.lastDayOfMonth());
    }
    
    /**
     * 获取星期值当前月的最后一天
     * @param localDateTime
     * @return
     */
    public static LocalDateTime lastDayOfMonth(LocalDateTime localDateTime){
        Objects.requireNonNull(localDateTime, "localDateTime");
        return localDateTime.with(TemporalAdjusters.lastDayOfMonth());
    }    
    
    /**
     * 获取星期值当前月的最后一天
     * @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){
        return localDateTime.with(TemporalAdjusters.next(dayOfWeek));
    }

    /**
     * 下一个星期几
     * @param date
     * @param dayOfWeek
     * @return
     */
    public static Date next(Date date, DayOfWeek dayOfWeek){
        return DateTimeConverterUtil.toDate(DateTimeConverterUtil.toLocalDate(date).with(TemporalAdjusters.next(dayOfWeek)));
    }
    
    
    /**
     * 上一个星期几
     * @param localDate
     * @param dayOfWeek
     * @return
     */
    public static LocalDate previous(LocalDate localDate, DayOfWeek dayOfWeek){
        Objects.requireNonNull(localDate, "localDate");
        return localDate.with(TemporalAdjusters.previous(dayOfWeek));
    }
    
    /**
     * 上一个星期几
     * @param localDateTime
     * @param dayOfWeek
     * @return
     */
    public static LocalDateTime previous(LocalDateTime localDateTime, DayOfWeek dayOfWeek){
        return localDateTime.with(TemporalAdjusters.previous(dayOfWeek));
    }

    /**
     * 上一个星期几
     * @param date
     * @param dayOfWeek
     * @return
     */
    public static Date previous(Date date, DayOfWeek dayOfWeek){
        return DateTimeConverterUtil.toDate(DateTimeConverterUtil.toLocalDate(date).with(TemporalAdjusters.previous(dayOfWeek)));
    }
    
    /**
     * 获下一个工作日
     * @param localDate
     * @return
     */
    public static LocalDate nextWorkDay(LocalDate localDate){
        Objects.requireNonNull(localDate, "localDate");
        return localDate.with(TemporalAdjusterExtension.nextWorkDay());
    }
    
    /**
     * 获下一个工作日
     * @param localDateTime
     * @return
     */
    public static LocalDateTime nextWorkDay(LocalDateTime localDateTime){
        Objects.requireNonNull(localDateTime, "localDateTime");
        return localDateTime.with(TemporalAdjusterExtension.nextWorkDay());
    }
    
    /**
     * 获下一个工作日
     * @param date
     * @return
     */
    public static Date nextWorkDay(Date date){
        return DateTimeConverterUtil.toDate(DateTimeConverterUtil.toLocalDate(date).with(TemporalAdjusterExtension.nextWorkDay()));
    }

 

测试代码:

 

    /**
     * Date其他常用计算
     */
    @Test
    public void dateCalculatorOtherTest(){
        Date date = new Date();
        System.out.println(date);
        System.out.println(DateTimeConverterUtil.toLocalDateTime(date));
        //获取星期值
        System.out.println(DateTimeCalculatorUtil.getDayOfWeek(date));
        //获取星期值当前月的最后一天
        System.out.println(DateTimeCalculatorUtil.lastDayOfMonth(date));
        //判断是否闰年
        System.out.println(DateTimeCalculatorUtil.isLeapYear(date));
        //获取月的天数
        System.out.println(DateTimeCalculatorUtil.lengthOfMonth(date));
        //获取月的天数
        System.out.println(DateTimeCalculatorUtil.lengthOfYear(date));
        //下一个星期一
        System.out.println(DateTimeCalculatorUtil.next(date, DayOfWeek.MONDAY));
        //上一个星期一
        System.out.println(DateTimeCalculatorUtil.previous(date, DayOfWeek.MONDAY));
        //获下一个工作日
        System.out.println(DateTimeCalculatorUtil.nextWorkDay(date));
    }
    
    /**
     * LocalDateTime其他常用计算
     */
    @Test
    public void dateCalculatorOtherTest2(){
        LocalDateTime ldt = LocalDateTime.now();
        System.out.println(ldt);
        
        //获取星期值
        System.out.println(DateTimeCalculatorUtil.getDayOfWeek(ldt));
        //获取星期值当前月的最后一天
        System.out.println(DateTimeCalculatorUtil.lastDayOfMonth(ldt));
        //判断是否闰年
        System.out.println(DateTimeCalculatorUtil.isLeapYear(ldt));
        //获取月的天数
        System.out.println(DateTimeCalculatorUtil.lengthOfMonth(ldt));
        //获取月的天数
        System.out.println(DateTimeCalculatorUtil.lengthOfYear(ldt));
        //下一个星期一
        System.out.println(DateTimeCalculatorUtil.next(ldt, DayOfWeek.MONDAY));
        //上一个星期一
        System.out.println(DateTimeCalculatorUtil.previous(ldt, DayOfWeek.MONDAY));
        //获下一个工作日
        System.out.println(DateTimeCalculatorUtil.nextWorkDay(ldt));
    }

 

测试结果:

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

 

源代码地址:https://github.com/xkzhangsan/xk-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!