JDK8 New Time API

本文介绍了Java8中的新时间API,包括LocalDateTime处理本地日期时间、Instant表示时间戳、ZoneId管理时区,以及DateTimeFormatter用于日期时间格式化。文章详细展示了这些类的使用示例和功能特性。
摘要由CSDN通过智能技术生成

New Time API

1 概述

之前时间API存在问题:线程安全问题、设计混乱。

本地化日期时间 API:

  • LocalDate: 本地日期
  • LocalTime: 本地时间
  • LocalDateTime: 本地日期时间

Instant:时间戳。

ZoneId:时区。

Date、Instant、LocalDateTime的转换。

DateTimeFormatter:格式化类。

2 LocalDateTime

表示本地日期时间,没有时区信息

代码演示:

package StageOne.day26.NewTimeAPI;

import java.time.LocalDateTime;

/**
 * @author 胡昊龙
 * @version 1.0
 * @description: TODO
 * @date 2024/1/23 9:18
 */
public class TestLocalDateTime {
    public static void main(String[] args) {
        //1 创建当前日期时间
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);
        //2 创建昨天
        LocalDateTime yesterday = now.minusDays(1);
        System.out.println(yesterday);
        //3 创建明天
        LocalDateTime tomorrow = now.plusDays(1);
        System.out.println(tomorrow);
        //4 创建任意时间
        LocalDateTime localDateTime = LocalDateTime.of(2001, 1, 20, 10, 20, 10);
        System.out.println(localDateTime);

        //获取日期时间部分
        System.out.println(now.getYear());
        System.out.println(now.getMonthValue());
        System.out.println(now.getDayOfMonth());
        System.out.println(now.getHour());
        System.out.println(now.getMinute());
        System.out.println(now.getSecond());

        //转成LocalDate
        System.out.println(now.toLocalDate());
        //转成LocalTime
        System.out.println(now.toLocalTime());
    }
}

3 Instant、ZoneId

Instant表示瞬间;和前面Date类似。

ZoneId表示时区信息。

  • Date、Instant、LocalDateTime的转换

代码演示:

TestInstant:

public class TestInstant {
    public static void main(String[] args) {
        //Instant 表示瞬时, 时间戳
        //当前时间
        Instant now = Instant.now();
        //打印格林尼治时间, 零时区
        System.out.println(now);
        //毫秒值 从1970年到现在的毫秒值
        System.out.println(now.toEpochMilli());
        System.out.println(System.currentTimeMillis());
        //昨天
        System.out.println(now.minusMillis(60 * 60 * 24 * 1000));
        //明天
        System.out.println(now.plusMillis(60 * 60 * 24 * 1000));
        //解析任意时间 格式规定: 2007-12-03T10:15:30.00Z
        Instant parsed = Instant.parse("2007-12-03T10:15:30.00Z");
        System.out.println(parsed);
        //计时
        Instant start = Instant.now();
        for (int i = 0; i < 9999; i++) {
            for (int j = 0; j < 9999; j++) {
                int sum = i + j;
            }
        }
        Instant end = Instant.now();
        System.out.println("用时: "+(end.toEpochMilli()-start.toEpochMilli()));
    }
}

TestZoneId:

public class TestZoneId {
    public static void main(String[] args) {
        //ZoneId 时区类
        //1 获取当前时区
        System.out.println(ZoneId.systemDefault());
        //2 获取其他时区
        Set<String> zoneIds = ZoneId.getAvailableZoneIds();
        System.out.println(zoneIds.size());
        for (String zoneId : zoneIds) {
            System.out.println(zoneId);
        }
    }
}

TestChange: Date、Instant、LocalDateTime的转换

public class TestChange {
    public static void main(String[] args) {
        //LocalDateTime-->Instant-->Date
        LocalDateTime now = LocalDateTime.now();
        Instant instant = now.atZone(ZoneId.systemDefault()).toInstant();
        Date date = Date.from(instant);
        System.out.println(date);

        //Date-->Instant-->LocalDateTime
        Instant instant1 = date.toInstant();
        LocalDateTime now1 = instant1.atZone(ZoneId.systemDefault()).toLocalDateTime();
        System.out.println(now1);
    }
}

4 DateTimeFormatter

DateTimeFormatter是时间格式化类。

代码演示:

public class TestDateTimeFormatter {
    public static void main(String[] args) {
        //创建一个格式化类
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        //操作1 时间转成字符串
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now.format(dtf));
        //操作2 字符串解析成时间
        String time = "2001-10-08 10:08:08";
        LocalDateTime dateTime = LocalDateTime.parse(time, dtf);
        System.out.println(dateTime);
    }
}
  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

贰贰柒丶阿拽

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

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

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

打赏作者

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

抵扣说明:

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

余额充值