Java 8 LocalDateTime has different results

前端 未结 2 2029
时光取名叫无心
时光取名叫无心 2021-01-17 07:52

I am trying to update some code to use Java 8\'s feature for parsing multiple date formats. my local time on my box is set to UTC-11.

the below cod

相关标签:
2条回答
  • 2021-01-17 08:16

    Solved, using OffsetDateTime as suggested in the accepted 'Answer':

    OffsetDateTime odt = OffsetDateTime.parse("2018-09-6T03:28:59.039-04:00");
    Date correctDate = Date.from( odt.toInstant());

    0 讨论(0)
  • 2021-01-17 08:24

    As soon as you parse your date string into a LocalDateTime the zone offset is lost because LocalDateTime does not hold any time zone or offset information. When you format the LocalDateTime to a string again, you'll only have the time as it was parsed without offset.

    The Documentation of LocalDateTime clearly explains this:

    This class does not store or represent a time-zone. Instead, it is a description of the date, as used for birthdays, combined with the local time as seen on a wall clock. It cannot represent an instant on the time-line without additional information such as an offset or time-zone.

    You should consider using OffsetDateTime or ZonedDateTime.

    0 讨论(0)
提交回复
热议问题