What's the difference between Instant and LocalDateTime?

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

I know that:

  • Instant is rather a \"technical\" timestamp representation (nanoseconds) for computing.
  • LocalDateTime i
4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-22 01:50

    You are wrong about LocalDateTime: it does not store any time-zone information and it has nanosecond precision. Quoting the Javadoc (emphasis mine):

    A date-time without a time-zone in the ISO-8601 calendar system, such as 2007-12-03T10:15:30.

    LocalDateTime is an immutable date-time object that represents a date-time, often viewed as year-month-day-hour-minute-second. Other date and time fields, such as day-of-year, day-of-week and week-of-year, can also be accessed. Time is represented to nanosecond precision. For example, the value "2nd October 2007 at 13:45.30.123456789" can be stored in a LocalDateTime.

    The difference between the two is that Instant represents an offset from the Epoch (01-01-1970) and, as such, represents a particular instant on the time-line. Two Instant objects created at the same moment in two different places of the Earth will have exactly the same value.

提交回复
热议问题