博客
关于我
Java8新日期和时间API
阅读量:406 次
发布时间:2019-03-05

本文共 4814 字,大约阅读时间需要 16 分钟。

Java8 新日期时间API详解

1 现有日期时间API的问题

Java的日期时间处理类在之前的版本中存在诸多痛点,主要体现在以下几个方面:

  • 线程安全问题DateCalendar 类均为可变类,其线程安全性需手动处理,Java8新引入的日期时间类则采用不可变设计,天然具备线程安全特性。

  • API设计不合理:不同包中的日期时间类(如java.util.Datejava.sql.Date)名称重复,且功能定位不清,java.util.Date 同时包含日期和时间,而java.sql.Date 仅处理日期,导致混淆。

  • 时区处理缺失:旧API缺乏国际化支持,CalendarTimeZone 类虽提供时区功能,但同样存在线程安全问题。

  • 2 Java8 新的日期和时间类

    Java8全面重新设计日期时间API,引入多个核心类别:

    编号 类名 描述
    1 LocalDate 只包含日期,格式如:2018-02-05
    2 LocalTime 仅包含时间,格式如:23:12:10
    3 LocalDateTime 同时包含日期和时间,格式如:2018-02-05 23:14:21
    4 Instant 时间戳,精度可达纳秒(如2024-03-20T12:34:56.789Z
    5 Duration 时间段,计算时间差(如1天、5小时)
    6 Period 以年、月、日为单位的时间段(如2年3个月)
    7 ZoneOffset 时区偏移量(如+8:00)
    8 ZonedDateTime 带时区的时间点(如2024-03-20T12:34:56+08:00)
    9 Clock 时钟实例,可指定时区(如获取上海时间)
    10 DateTimeFormatter 时间格式化工具(如YYYY-MM-DDHH:mm:ss.SSS

    3 LocalDate、LocalTime和LocalDateTime

    3.1 LocalDate

    LocalDate表示具体日期,不含时间和时区。

    LocalDate now = LocalDate.now(); // 获取当前日期
    LocalDate localDate = LocalDate.of(2017, 1, 4); // 初始化日期:2017-01-04
    int year = localDate.getYear(); // 年份:2017
    Month month = localDate.getMonth(); // 月份:JANUARY
    int dayOfMonth = localDate.getDayOfMonth(); // 当月的第几天:4
    DayOfWeek dayOfWeek = localDate.getDayOfWeek(); // 一周中的第几天:WEDNESDAY
    int length = localDate.lengthOfMonth(); // 月份的天数:31
    boolean leapYear = localDate.isLeapYear(); // 是否为闰年:false

    3.2 LocalTime

    LocalTime只包含时间,不含日期。

    LocalTime now = LocalTime.now(); // 获取当前时间
    LocalTime localTime = LocalTime.of(17, 23, 52); // 初始化时间:17:23:52
    int hour = localTime.getHour(); // 时:17
    int minute = localTime.getMinute(); // 分:23
    int second = localTime.getSecond(); // 秒:52

    3.3 LocalDateTime

    LocalDateTime同时包含日期和时间。

    LocalDateTime now = LocalDateTime.now(); // 当前日期时间
    LocalDateTime ldt1 = LocalDateTime.of(2017, Month.JANUARY, 4, 17, 23, 52); // 2017-01-04 17:23:52
    LocalDate localDate = LocalDate.now();
    LocalTime localTime = LocalTime.now();
    LocalDateTime ldt2 = localDate.atTime(localTime); // 将日期与时间合并
    LocalDate localDate1 = ldt1.toLocalDate(); // 转化为日期
    LocalTime localTime1 = ldt1.toLocalTime(); // 转化为时间

    4 Instant、Duration和Period

    4.1 Instant

    Instant表示时间戳,支持纳秒精度。

    Instant now = Instant.now(); // 获取当前时间戳
    long l = now.toEpochMilli(); // 转换为毫秒数

    4.2 Duration

    Duration表示时间段,支持天、小时、分钟、秒等单位。

    LocalDateTime from = LocalDateTime.of(2017, Month.JANUARY, 5, 10, 7, 0); // 2017-01-05 10:07:00
    LocalDateTime to = LocalDateTime.of(2017, Month.FEBRUARY, 5, 10, 7, 0); // 2017-02-05 10:07:00
    Duration duration = Duration.between(from, to); // 计算时间差
    long days = duration.toDays(); // 总天数
    long hours = duration.toHours(); // 小时数
    long minutes = duration.toMinutes(); // 分钟数
    long seconds = duration.getSeconds(); // 秒数
    long milliSeconds = duration.toMillis(); // 毫秒数
    long nanoSeconds = duration.toNanos(); // 纳秒数

    4.3 Period

    Period用于表示年月日级别的时间段。

    Period period2 = Period.of(2, 3, 6); // 2年3个月6天
    Period period = Period.between(LocalDate.of(2017, 1, 5), LocalDate.of(2018, 2, 5)); // 2017-01-05 到 2018-02-05

    5 日期的操作和格式化

    5.1 日期增减操作

    LocalDate date = LocalDate.of(2017, 1, 5); // 2017-01-05
    LocalDate date1 = date.withYear(2016); // 2016-01-05
    LocalDate date2 = date.withMonth(2); // 2017-02-05
    LocalDate date3 = date.withDayOfMonth(1); // 2017-01-01
    LocalDate date4 = date.plusYears(1); // 2018-01-05
    LocalDate date5 = date.minusMonths(2); // 2016-11-05
    LocalDate date6 = date.plus(5, ChronoUnit.DAYS); // 2017-01-10
    // 特殊操作
    LocalDate date7 = date.with(nextOrSame(DayOfWeek.SUNDAY)); // 下一个星期日
    LocalDate date9 = date.with(lastInMonth(DayOfWeek.SATURDAY)); // 本月最后一个星期六

    5.2 时间格式化

    LocalDateTime dateTime = LocalDateTime.now();
    String strDate1 = dateTime.format(DateTimeFormatter.BASIC_ISO_DATE); // 20170105
    String strDate2 = dateTime.format(DateTimeFormatter.ISO_LOCAL_DATE); // 2017-01-05
    String strDate3 = dateTime.format(DateTimeFormatter.ISO_LOCAL_TIME); // 14:20:16.998
    String strDate4 = dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")); // 2017-01-05
    String strDate5 = dateTime.format(DateTimeFormatter.ofPattern("今天是:YYYY年 MMMM DD日 E", Locale.CHINESE)); // 今天是:2017年 一月 05日 星期四
    // 解析日期
    String strDate6 = "2017-01-05";
    LocalDate date = LocalDate.parse(strDate6, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
    String strDate7 = "2017-01-05 12:30:05";
    LocalDateTime dateTime1 = LocalDateTime.parse(strDate7, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

    6 时区

    时区处理采用新的API,支持国际化。

    ZoneId shanghaiZoneId = ZoneId.of("Asia/Shanghai"); // 上海时区
    ZoneId systemZoneId = ZoneId.systemDefault(); // 系统默认时区
    String zoneIds = ZoneId.getAvailableZoneIds(); // 获取所有时区ID
    LocalDateTime localDateTime = LocalDateTime.now();
    ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, shanghaiZoneId); // 包含时区日期时间
    // 通过偏移量表示时区
    ZoneOffset zoneOffset = ZoneOffset.of("+09:00");
    LocalDateTime localDateTime = LocalDateTime.now();
    OffsetDateTime offsetDateTime = OffsetDateTime.of(localDateTime, zoneOffset);

    转载地址:http://uphwz.baihongyu.com/

    你可能感兴趣的文章
    NFS的常用挂载参数
    查看>>
    NFS网络文件系统
    查看>>
    nft文件传输_利用remoting实现文件传输-.NET教程,远程及网络应用
    查看>>
    NFV商用可行新华三vBRAS方案实践验证
    查看>>
    ng build --aot --prod生成文件报错
    查看>>
    ng 指令的自定义、使用
    查看>>
    nghttp3使用指南
    查看>>
    Nginx
    查看>>
    nginx + etcd 动态负载均衡实践(三)—— 基于nginx-upsync-module实现
    查看>>
    nginx + etcd 动态负载均衡实践(二)—— 组件安装
    查看>>
    nginx + etcd 动态负载均衡实践(四)—— 基于confd实现
    查看>>
    Nginx + Spring Boot 实现负载均衡
    查看>>
    Nginx + uWSGI + Flask + Vhost
    查看>>
    Nginx - Header详解
    查看>>
    Nginx - 反向代理、负载均衡、动静分离、底层原理(案例实战分析)
    查看>>
    nginx 1.24.0 安装nginx最新稳定版
    查看>>
    nginx 301 永久重定向
    查看>>
    nginx css,js合并插件,淘宝nginx合并js,css插件
    查看>>
    Nginx gateway集群和动态网关
    查看>>
    Nginx Location配置总结
    查看>>