时间处理的未来:Java 8全新日期与时间API完全解析

在这里插入图片描述

一、改进背景

Java 8针对时间处理进行了全面的改进,重新设计了所有日期时间、日历及时区相关的 API。并把它们都统一放置在 java.time 包和子包下。

Java5的不足之处

  1. 非线程安全java.util.Date 并不是线程安全的,在使用这个类时必须自己处理多线程并发问题。
  2. 设计不佳 :日期和日期格式化分布在多个包中,java.util.Date 的默认日期,年是从1900开始,月从 1 开始,日从 0 开始,没有统一性。而且 Date 类也缺少直接操作日期的相关方法。
  3. 时区处理困难:因为设计不佳,不得不编写大量代码来处理时区问题。

Java8的改进方案

  1. 线程安全:新的日期时间API是线程安全的不仅没有setter方法,而且任何对实例的变更都会返回一个新的实例而保证原来的实例不变。
  2. 日期修改:新的日期时间API提供了大量的方法,用于修改日期时间的各个部分,并返回一个新的实例。
  3. : 在时区方面,新的日期时间API引入了域这个概念。
  4. 组合拆分:针对原来复杂的 API 进行重新组合和拆分,分成了好多个类。

二、本地日期时间

  1. LocalDate: 用于表示不含时区的日期,例如:2024-07-06。

    import java.time.LocalDate;
    import java.time.Month;
    
    public class LocalDateExample {
        public static void main(String[] args) {
            // 获取当前日期
            LocalDate today = LocalDate.now();
            System.out.println("当前日期: " + today);
    
            // 创建指定日期
            LocalDate specificDate = LocalDate.of(2024, Month.JULY, 6);
            System.out.println("指定日期: " + specificDate);
    
            // 日期操作示例
            LocalDate tomorrow = today.plusDays(1);
            System.out.println("明天的日期: " + tomorrow);
        }
    }
    
    // 输出
    当前日期: 2024-07-06
    指定日期: 2024-07-06
    明天的日期: 2024-07-07
    
  2. LocalTime: 用于表示不含时区的时间,例如:10:30:15.。

    import java.time.LocalTime;
    
    public class LocalTimeExample {
        public static void main(String[] args) {
            // 获取当前时间
            LocalTime currentTime = LocalTime.now();
            System.out.println("当前时间: " + currentTime);
    
            // 创建指定时间
            LocalTime specificTime = LocalTime.of(14, 30, 45);
            System.out.println("指定时间: " + specificTime);
    
            // 时间操作示例
            LocalTime laterTime = currentTime.plusHours(2);
            System.out.println("两小时后的时间: " + laterTime);
        }
    }
    
    // 输出
    当前时间: 19:44:24.397
    指定时间: 14:30:45
    两小时后的时间: 21:44:24.397
    
  3. LocalDateTime: 用于表示不含时区的日期时间,例如:2024-07-06T10:30:15。

    import java.time.LocalDateTime;
    import java.time.Month;
    
    public class LocalDateTimeExample {
        public static void main(String[] args) {
            // 获取当前日期时间
            LocalDateTime currentDateTime = LocalDateTime.now();
            System.out.println("当前日期时间: " + currentDateTime);
    
            // 创建指定日期时间
            LocalDateTime specificDateTime = LocalDateTime.of(2024, Month.JULY, 6, 14, 30, 45);
            System.out.println("指定日期时间: " + specificDateTime);
    
            // 日期时间操作示例
            LocalDateTime laterDateTime = currentDateTime.plusDays(1).plusHours(2);
            System.out.println("明天两小时后的日期时间: " + laterDateTime);
        }
    }
    
    // 输出
    当前日期时间: 2024-07-06T19:45:55.358
    指定日期时间: 2024-07-06T14:30:45
    明天两小时后的日期时间: 2024-07-07T21:45:55.358
    

三、时区日期时间

在Java 8的新日期时间API中,除了处理本地日期时间外,还引入了处理时区日期时间的类,主要是 ZonedDateTimeZoneId

  1. ZonedDateTime :处理带时区的日期时间的类,它包含了本地日期时间和对应的时区信息。
import java.time.*;

public class ZonedDateTimeExample {
    public static void main(String[] args) {
        // 获取当前日期时间
        LocalDateTime localDateTime = LocalDateTime.now();

        // 创建 ZonedDateTime 对象
        // 时区ID
        ZoneId zoneId = ZoneId.of("Asia/Shanghai");
        ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, zoneId);

        System.out.println("当前日期时间和时区: " + zonedDateTime);

        // 获取其它时区的日期时间
        ZoneId newYorkZoneId = ZoneId.of("America/New_York");
        ZonedDateTime newYorkDateTime = zonedDateTime.withZoneSameInstant(newYorkZoneId);
        System.out.println("纽约的日期时间: " + newYorkDateTime);

        // 当前的日期时间
        ZonedDateTime now = ZonedDateTime.now();
        System.out.println("当前日期时间是:" + now);
        System.out.println("当前时区是: " + now.getZone());

        LocalDate date = now.toLocalDate();
        System.out.println("本地日期是:" + now);

        LocalTime time = now.toLocalTime();
        System.out.println("本地时间是:" + time);
    }
}

// 输出
当前日期时间和时区: 2024-07-06T20:07:56.439+08:00[Asia/Shanghai]
纽约的日期时间: 2024-07-06T08:07:56.439-04:00[America/New_York]
当前日期时间是:2024-07-06T20:07:56.508+08:00[GMT+08:00]
当前时区是: GMT+08:00
本地日期是:2024-07-06T20:07:56.508+08:00[GMT+08:00]
本地时间是:20:07:56.508
  1. ZoneId:表示时区的标识符,可以通过它来获取具体的时区信息。
public class ZoneIdExample {
    public static void main(String[] args) {
        // 获取所有的可用时区ID
        System.out.println("所有可用的时区ID: " + ZoneId.getAvailableZoneIds());

        // 获取特定时区的信息
        ZoneId zoneId = ZoneId.of("Asia/Tokyo");
        System.out.println("时区ID为 Asia/Tokyo 的信息: " + zoneId);
        
        // 获取当前时区
        ZoneId currentZone = ZoneId.systemDefault();
        System.out.println("当前时区是: " + currentZone);
    }
}

// 输出
所有可用的时区ID: [Asia/Aden, America/Cuiaba,……]
时区IDAsia/Tokyo 的信息: Asia/Tokyo
当前时区是: GMT+08:00

四、格式化

DateTimeFormatter 类用于格式化和解析日期时间对象,它提供了多种预定义的格式化方式,也支持自定义格式。

import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class DateTimeFormatterExample {
    public static void main(String[] args) {
        // 当前时间
        ZonedDateTime now = ZonedDateTime.now();
        System.out.println("当前时间是: " + now);

        System.out.println("另一种表示形式:" + now.format(DateTimeFormatter.RFC_1123_DATE_TIME));

        // 创建一个 DateTimeFormatter 对象并使用预定义格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        // 格式化 LocalDateTime 对象
        String formattedDateTime = now.format(formatter);
        System.out.println("格式化后的日期时间: " + formattedDateTime);

        // 解析字符串到 LocalDateTime 对象
        LocalDateTime parsedDateTime = LocalDateTime.parse(formattedDateTime, formatter);
        System.out.println("解析后的日期时间: " + parsedDateTime);
    }
}

Java 8 提供了许多预定义的格式化模式

  • yyyy-MM-dd
  • yyyy-MM-dd HH:mm:ss
  • MMM dd, yyyy HH:mm:ss
  • 等等…

可以根据需要选择合适的格式化模式来格式化或解析日期时间字符串,使用自定义格式时要确保格式与输入字符串的格式匹配,否则会导致解析失败或异常 。

闲暇是霓裳,不宜常穿用

  • 133
    点赞
  • 120
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 210
    评论
评论 210
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

忆~遂愿

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

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

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

打赏作者

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

抵扣说明:

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

余额充值