java.time包阅读笔记

预备知识

java.time包概述

  • 所有的class都是immutable和thread-safe

各个包之间的关系

  • java.time.temporal提供对时间的低层级访问
  • java.time.format提供格式
  • java.time.chrono包含日历无关(calendar neutral)的API

Dates and Times

Instant

  • a numeric timestamp

LocalDate

LocalTime

LocalDateTime

ZonedDateTime

  • 一般不需要使用

  • "Where possible, it is recommended to use a simpler class without a time-zone. The widespread use of time-zones tends to add considerable complexity to an application."

Duration and Period

Duration

  • 以纳秒表示的时间差

Period

  • 以对人类有意义的时间单位(年月日)表示的时间差

Additional value types

Month

  • 例如,’DECEMBER‘

DayOfWeek

  • 例如,’THUESDAY‘

Year

  • 例如,’2021‘

YearMonth

  • 例如,’2021-07‘

MonthYear

  • 例如,‘--07-01’

OffsetTime

  • 例如,‘15:07:46.964842600+08:00’
  • 表示15:07分,目前处于东八区

Design Note

  • Instant是Java.util.Date的最接近的等价类

  • ZonedDateTime是java.util.GregorianCalendar的最接近的等价类

  • OffsetTime和OffsetDateTime主要用于网络协议和数据库访问,因为大多数数据库无法将诸如‘Europe/Paris’这样的时区信息进行自动转换并存储,但是对于偏移却可以

  • 统一的方法接口

    • of - static factory method
      parse - static factory method focussed on parsing
      get - gets the value of something
      is - checks if something is true
      with - the immutable equivalent of a setter
      plus - adds an amount to an object
      minus - subtracts an amount from an object
      to - converts this object to another type
      at - combines this object with another, such as date.atTime(time)
      
  • 数据以ISO Calendar标准存储

  • 不同日期之间的转换需要使用到java.time.chrono包

原文地址:https://www.cnblogs.com/N3ptuner/p/14954754.html