JDK8 日期时间 API 的简单使用

本文比较了JDK1.0的Date类与JDK8的LocalDateTime类在处理日期和时间方面的差异,指出Date类的问题和新版本的改进,并强调了Java8中final和不可变对象在解决线程安全问题上的优势。
摘要由CSDN通过智能技术生成

JDK1.0版本(Date)

Date 类中的所有方法从 JDK1.1 开始便被弃用。

存在的问题:

  • 计算差值时不准确、麻烦、设计反人类
  • SimpleDateFormat 存在线程安全问题,即多线程环境下需要考虑并发问题
public class DateDemo {

    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        // 这里的设置就很反人类
        calendar.set(2000, Calendar.JANUARY, 1);
        long beginMill = calendar.getTimeInMillis();

        Date date = new Date();
        long endMill = date.getTime();

        long days = (endMill - beginMill) / 1000 / 3600 / 24;
        System.out.println("days = " + days);

        long years = days / 365;
        System.out.println("years = " + years);
    }
}

JDK 8 版本(LocalDateTime)

注意:

  • 出现过上面的结果和这里的结果存在一天之差?为什么?谁算错了?
  • 但这种错误在几分钟后就消失了。大概是 2023-11-25 22:10:00 左右发生这种情况

在学习的视频演示案例中,也出现了这个问题,是 Date 类的问题

public class LocalDateDemo {
    public static void main(String[] args) {
        LocalDate begin = LocalDate.of(2000, 1, 1);
        LocalDate end = LocalDate.now();

        long days = ChronoUnit.DAYS.between(begin, end);
        System.out.println("days = " + days);
        long years = ChronoUnit.YEARS.between(begin, end);
        System.out.println("years = " + years);
    }
}

##时刻(时间点)

描述
Instant1. 对时间轴上某个时刻的描述
2. 各种时间类型的转换媒介
LocalDate
LocalTime
LocalDateTime
ZonedDateTime

共性:

  • java.time 包下的这些类都是 final 修饰的,final 修饰也表明对象是不可变的,因此解决线程安全问题。
  • 这些类属于工具类,设计者将构造器屏蔽掉,因此不能够使用 new 来创建对象,而是调用静态方法

of 方法:指定日期时间

    public static void main(String[] args) {
        // Duration和Period没有now()方法
        Instant instant = Instant.now();
        System.out.println("instant = " + instant);

        LocalDate localDate = LocalDate.now();
        System.out.println("localDate = " + localDate);

        // 时分秒+纳秒,没有毫秒
        LocalTime localTime = LocalTime.now();
        System.out.println("localTime = " + localTime);

        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println("localDateTime = " + localDateTime);

        ZonedDateTime zonedDateTime = ZonedDateTime.now();
        System.out.println("zonedDateTime = " + zonedDateTime);
    }

plus、minus 方法:增加/减少

    public static void main(String[] args) {
        // 处理了闰年等问题
        LocalDateTime localDateTime1 = LocalDateTime.of(2000, 2, 28, 12, 0, 0);
        LocalDateTime plusLocalDateTime1 = localDateTime1.plusWeeks(1);
        System.out.println("plusLocalDateTime1 = " + plusLocalDateTime1);

        LocalDateTime localDateTime2 = LocalDateTime.of(2001, 2, 28, 12, 0, 0);
        LocalDateTime plusLocalDateTime2 = localDateTime2.plusWeeks(1);
        System.out.println("plusLocalDateTime2 = " + plusLocalDateTime2);

        // 采用链式编程来进行多次的计算
        LocalDateTime ddl1 = LocalDateTime.now().plusYears(2).plusMonths(3).plusDays(8);
        System.out.println("ddl1 = " + ddl1);

        // 添加年月的逻辑是直接在对应属性上添加,然后避免越界
        Period period = Period.of(1, 0, 0);
        LocalDate ddl2 = LocalDate.of(2000, 2, 29).plus(period);
        System.out.println("ddl2 = " + ddl2);//这里的结果是2001-02-28
    }

时段(差值)

日期时间差值计算类描述
Duration用于计算 LocalTime 之间的差值
Period用于计算 LocalDate 之间的差值
    public static void main(String[] args) {
        // Period用于计算LocalDate之间的差值
        LocalDate beginDate = LocalDate.of(2000, 1, 1);
        LocalDate endDate = LocalDate.of(2023, 11, 25);
        Period period = Period.between(beginDate, endDate);
        System.out.println("period = " + period);
        int days = period.getDays();
        System.out.println("days = " + days);

        // Duration用于计算LocalTime之间的差值
        LocalTime beginTime = LocalTime.of(8, 53, 30);
        LocalTime endTime = LocalTime.of(23, 12, 9);
        Duration duration = Duration.between(beginTime, endTime);
        System.out.println("duration = " + duration);
        long minutes = duration.toMinutes();
        System.out.println("minutes = " + minutes);
        // Duration用于LocalDate会报错
    }

时区

时区类描述
ZoneId时区信息
ZonedDateTime某个时区的 LocalDateTime

既可以获取当前的时区,也可以进行不同时区的时间转换

    public static void main(String[] args) {
        // 所有的时区信息
        Set<String> zoneIds = ZoneId.getAvailableZoneIds();
        zoneIds.forEach(System.out::println);

        // 获取当前系统的默认时区
        ZoneId defaultZoneId = ZoneId.systemDefault();
        System.out.println("defaultZoneId = " + defaultZoneId);

        System.out.println("====================================================");

        // LocalDateTime:不包含时区信息。7点10分
        LocalDateTime localDateTime = LocalDateTime.of(2023, 11, 25, 7, 10);

        // ZonedDateTime:包含时区信息。
        ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.of("Asia/Shanghai"));
        System.out.println("zonedDateTime = " + zonedDateTime);

        // 时区信息转换:上海的7点10分 != 洛杉矶的7点10分
        ZonedDateTime otherZonedDateTime = zonedDateTime.withZoneSameInstant(ZoneId.of("America/Los_Angeles"));
        System.out.println("otherZonedDateTime = " + otherZonedDateTime);

        // 转换成其它时区(二)
        ZonedDateTime utcZonedDateTime = zonedDateTime.withZoneSameInstant(ZoneId.of("Etc/UTC"));
        System.out.println("utcZonedDateTime = " + utcZonedDateTime);
    }
  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值