Java三代日期类

日期类

在Java中,有三代日期类:java.util.Datejava.util.Calendarjava.time包下的日期类。这三代日期类在不同的Java版本中引入和改进,每一代都提供了更强大和更易于使用的日期和时间处理功能。

第一代日期类

第一代日期类:java.util.Date
java.util.Date是Java最早引入的日期类,它表示从1970年1月1日开始的毫秒数。尽管它提供了基本的日期和时间操作方法,但它在设计上存在一些问题。其中一个主要问题是它的可变性,即一旦创建了一个Date对象,就无法更改其值。此外,它的大部分方法已被标记为过时,不推荐使用。

第二代日期类

第二代日期类:java.util.Calendar
java.util.Calendar是Java 1.1版本引入的日期类,用于弥补Date类的不足。Calendar类提供了更多的日期和时间操作方法,如获取特定日期的年、月、日、时、分、秒等。它还提供了一些用于日期和时间计算的方法,如添加或减去特定的时间单位。但是,Calendar类的设计也存在一些问题。它的月份从0开始,即0表示一月,11表示十二月,这在使用时容易引起混淆。此外,Calendar类也不是线程安全的。

第三代日期类

第三代日期类:java.time包下的日期类
java.time包是在Java 8中引入的,它提供了一组全新的日期和时间类,用于取代旧的DateCalendar类。这些新的日期类是不可变的,线程安全的,并且提供了更加清晰和易于使用的API。主要的日期类包括:

  • LocalDate:表示日期,如年、月、日。
  • LocalTime:表示时间,如时、分、秒。
  • LocalDateTime:表示日期和时间,如年、月、日、时、分、秒。
  • ZonedDateTime:表示带有时区的日期和时间。
  • Instant:表示时间戳,精确到纳秒级别。
  • Duration:表示时间的持续时间。
  • Period:表示日期的持续时间。

示例代码:

public class Test1 {
    public static void main(String[] args) {
        // 创建 LocalDate 对象表示当前日期(年月日)
        LocalDate currentDate = LocalDate.now();
        System.out.println("当前日期: " + currentDate);

        // 创建 LocalTime 对象表示当前时间(时分秒)
        LocalTime currentTime = LocalTime.now();
        System.out.println("当前时间: " + currentTime);

        // 创建 LocalDateTime 对象表示当前日期和时间(年月日时分秒)
        LocalDateTime currentDateTime = LocalDateTime.now();
        System.out.println("当前日期和时间: " + currentDateTime);

        // 创建带有时区的 ZonedDateTime 对象
        ZonedDateTime zonedDateTime = ZonedDateTime.now();
        System.out.println("当前日期和时间(带时区): " + zonedDateTime);

        // 创建 Instant 对象表示当前时间戳
        Instant instant = Instant.now();
        System.out.println("当前时间戳: " + instant);
        
        // 转换为以毫秒为单位的时间戳
        long epochMilli = instant.toEpochMilli(); 
        System.out.println("毫秒时间戳: " + epochMilli);

        // 计算两个时间之间的持续时间
        LocalDateTime startDateTime = LocalDateTime.of(2024, 1, 1, 0, 0, 0);
        LocalDateTime endDateTime = LocalDateTime.of(2024, 1, 1, 12, 0, 0);
        Duration duration = Duration.between(startDateTime, endDateTime);
        System.out.println("时间间隔: " + duration);

        // 计算两个日期之间的持续时间
        LocalDate startDate = LocalDate.of(2024, 1, 1);
        LocalDate endDate = LocalDate.of(2024, 12, 31);
        Period period = Period.between(startDate, endDate);
        System.out.println("日期间隔: " + period);
    }
}

输出:

当前日期: 2024-03-07
当前时间: 09:19:30.221607600
当前日期和时间: 2024-03-07T09:19:30.221607600
当前日期和时间(带时区): 2024-03-07T09:19:30.222605800+08:00[Asia/Shanghai]
当前时间戳: 2024-03-07T01:19:30.222605800Z
毫秒时间戳: 1709775409194
时间间隔: PT12H
日期间隔: P11M30D

LocalDateTime方法

LocalDateTime currentDateTime = LocalDateTime.now();

System.out.println("年=" + currentDateTime.getYear());
System.out.println("月=" + currentDateTime.getMonth());
System.out.println("月=" + currentDateTime.getMonthValue());
System.out.println("日=" + currentDateTime.getDayOfMonth());
System.out.println("时=" + currentDateTime.getHour());
System.out.println("分=" + currentDateTime.getMinute());
System.out.println("秒=" + currentDateTime.getSecond());

输出:

=2024=MARCH=3=7=9=22=14

LocalDateTime格式化

LocalDateTime currentDateTime = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); // 格式化LocalDateTime
System.out.println("格式化后:"+currentDateTime.format(formatter));

输出:

格式化后:2024-03-07 09:23:22

日期与时间戳的转换

Date转换为时间戳
LocalDateTime dateTime = LocalDateTime.now();
long timestamp = dateTime.toEpochSecond(ZoneOffset.UTC);
System.out.println("LocalDateTime转化为时间戳:"+timestamp);

输出:

Date转化为时间戳:2024-03-07T01:37:43.387Z
时间戳转换为Date
Instant now = Instant.now();
Date date = Date.from(now);
System.out.println("时间戳转换为Date:"+now);

输出:

时间戳转换为Date:2024-03-07T01:39:23.786778200Z
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

林小果呀

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

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

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

打赏作者

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

抵扣说明:

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

余额充值