What's the difference between Instant and LocalDateTime?

后端 未结 4 944
野的像风
野的像风 2020-11-22 01:18

I know that:

  • Instant is rather a \"technical\" timestamp representation (nanoseconds) for computing.
  • LocalDateTime i
4条回答
  •  北恋
    北恋 (楼主)
    2020-11-22 02:05

    One main difference is the Local part of LocalDateTime. If you live in Germany and create a LocalDateTime instance and someone else lives in USA and creates another instance at the very same moment (provided the clocks are properly set) - the value of those objects would actually be different. This does not apply to Instant, which is calculated independently from time zone.

    LocalDateTime stores date and time without timezone, but it's initial value is timezone dependent. Instant's is not.

    Moreover, LocalDateTime provides methods for manipulating date components like days, hours, months. An Instant does not.

    apart from the nanosecond precision advantage of Instant and the time-zone part of LocalDateTime

    Both classes have the same precision. LocalDateTime does not store timezone. Read javadocs thoroughly, because you may make a big mistake with such invalid assumptions: Instant and LocalDateTime.

提交回复
热议问题