java基础--22(java8新特性--时间和日期的API)

本文深入探讨了Java中新的时间日期API,包括LocalDate、LocalTime、LocalDateTime等类的使用,以及如何处理时间间隔、时区转换和格式化。通过实例展示了如何比较和调整日期时间,控制两个时间点之间的间隔,确保时间操作的正确性和效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.几个重要的类

     LocalDate,LocalDateTime,LocalTime,Instant,Duration 以及 Period,TemporalAdjusters,DateTimeFormatter

2.本地时间

     LocalDate,LocalDateTime,LocalTime这三个为本地时间,生日,假日,计划等通常都会表示成本地时间,创建和使用的方法都差不多。

     LocalDate的作用:创建时间,增加或减去年月日天周,修改年月日,获取年月日周,判断是否为闰年,比较两个时间

   将时间格式化后进行比较:

 public void checkSendDate(Date sendDate) {
        //发送时间选择将来
        Instant instant = sendDate.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();

        LocalDateTime nowLocalDateTime = LocalDateTime.now();
        LocalDateTime sendLocalDateTime = instant.atZone(zoneId).toLocalDateTime();

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
        String strNowLocalDateTime = nowLocalDateTime.format(formatter);
        String strSendLocalDateTime = sendLocalDateTime.format(formatter);

        nowLocalDateTime = LocalDateTime.parse(strNowLocalDateTime, formatter);
        sendLocalDateTime = LocalDateTime.parse(strSendLocalDateTime, formatter);
        if (sendLocalDateTime.isBefore(nowLocalDateTime)) {
            throw new CommonException("error.send.time");
        }

    }

 控制两个时间之间的间隔:

  public void checkQueryByDate(ArtCalendarEventVO artCalendarEventVO) {
        ZoneId zoneId = ZoneId.systemDefault();
        if (artCalendarEventVO.getStartDate() != null && artCalendarEventVO.getEndDate() != null) {
            LocalDate startDate = artCalendarEventVO.getStartDate().toInstant().atZone(zoneId).toLocalDate();
            LocalDate endDate = artCalendarEventVO.getEndDate().toInstant().atZone(zoneId).toLocalDate();
            if (endDate.toEpochDay() - startDate.toEpochDay() > 180) {
                throw new CommonException("error.lastTime.too.long");
            }
        }
        if (artCalendarEventVO.getStartDate() != null && artCalendarEventVO.getEndDate() == null) {
            LocalDateTime localDateTime = artCalendarEventVO.getStartDate().toInstant().atZone(zoneId).toLocalDateTime();
            artCalendarEventVO.setEndDate(Date.from(localDateTime.plusDays(180).atZone(zoneId).toInstant()));
        }
        if (artCalendarEventVO.getEndDate() != null && artCalendarEventVO.getStartDate() == null) {
            LocalDateTime localDateTime = artCalendarEventVO.getEndDate().toInstant().atZone(zoneId).toLocalDateTime();
            artCalendarEventVO.setStartDate(Date.from(localDateTime.minusDays(180).atZone(zoneId).toInstant()));
        }
    }

3.两个表示绝对时间的类

时间线Instant,表示时间线上的某一时刻,有最大值,最小值,这个类还可以比较,并且他得到的时间是所谓的带时区的UTC时间。

Duration:表示两个绝对时间之间的度量

 这两个类都是final类,所做的时间的操作都是返回一个新的对象。

4.本地时间

   人类时间LocalDate带有年月日的日期。

   

   除了可以在LocalDate对象上加减时间,时间段以外,还可以通过isBefor,isAfter方法比较时间 以及判断是否闰年。

   Period表示两个本地时间的间隔。

   TemporalAdjusters 日期调整期类, 除了提供的默认的调整器类,还可以通过实现TemporalAdjuster接口来自己写一个调整器。

   

   从给定日期起下一个星期三的日期。

   现在自己写一个日期调整器获取下一个工作日:

   

   可以发现TemporalAdjuster是一个函数式的接口,所以可以传一个lambda表达式。

  

 第二个本地时间LocalTime,表示当日的时刻

LocalDateTime表示日期和时间。

 

5.时区

  如果想要时间带上时区,那么就要使用ZonedDateTime类,相当于Calendar

  

6.格式化

  DateTimeFormatter

 

 

 

 

 

 

 

 

 

 

 

 

 

参考博客:

https://www.cnblogs.com/wupeixuan/p/11511915.html?utm_source=gold_browser_extension

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

时空恋旅人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值