-1

I have a timestamp 2019-09-02T09:42:05+0000 . What does the last part +0000 mean and how to convert this timestamp to LocalDateTime object?

LocalDateTime.parse("2019-09-02T09:42:05", DateTimeFormatter.ISO_LOCAL_DATE_TIME)

works perfectly. I am unable to find appropriate formatter for 2019-09-02T09:42:05+0000

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
user109447
  • 1,069
  • 1
  • 10
  • 20
  • 3
    `+0000` is a timezone offset. I suggest you carefully read the Wikipedia page on [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) date formats. – Jim Garrison Sep 04 '19 at 18:37
  • Possible duplicate of [Convert date into AEST using java](https://stackoverflow.com/questions/48412345/convert-date-into-aest-using-java) – Ole V.V. Sep 04 '19 at 18:40
  • Since your string contains a UTC offset (that’s what +0000 is), `OffsetDateTime` is a better matxh than `LocalDateTime` and the one I recommend you parse into. Subsequent conversion to `LocalDateTime` is possible if you know in which time zone you want your `LocalDateTime`. – Ole V.V. Sep 04 '19 at 18:42
  • Subsequent conversion to `LocalDateTime` is always possible. Do you mean `ZonedDateTime`? @OleV.V. – Sweeper Sep 04 '19 at 18:45
  • @Sweeper I mean you need to assume a time zone (or a UTC offset) for that conversion, and if you assume a different time zone from the intended, you will most likely get an incorrect result. – Ole V.V. Sep 04 '19 at 18:46
  • 1
    Yeah, the timestamp is telling you that it's a UTC (or GMT) time. Parse it as what it says it is. Parsing code like that will also work with timestamps that use other offsets. – Andrew Koster Sep 04 '19 at 19:02

2 Answers2

1

Use the ISO_OFFSET_DATE_TIME option instead. That time stamp has an offset, so you need to use the right formatter.

LocalDateTime.parse("2019-09-02T09:42:05+0000", DateTimeFormatter.ISO_OFFSET_DATE_TIME)

Documentation: https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ISO_OFFSET_DATE_TIME

Taugenichts
  • 1,307
  • 12
  • 19
1

Offset-from-UTC

What does the last part +0000 mean

That part is an abbreviated version of +00:00. This is a number of hours and minutes (and seconds optionally). That number is the amount of time the date-time is ahead of UTC (to the east) or behind UTC (to the west). An offset of zero means UTC itself itself.

By the way, an offset of zero happens to be used by the time zone of Iceland.

LocalDateTime does not represent a moment

how to convert this timestamp to LocalDateTime object?

You do not.

LocalDateTime is the wrong class. Your input string represents a moment, a specific point on the timeline. In contrast, LocalDateTime, lacking any concept of offset or zone, cannot represent a moment. This is noted in the class Javadoc.

So when do we use LocalDateTime with only its date and time-of-day? For three reasons:

  • We are describing a time-of-day day to be applied individually in multiple time zones. Perhaps we want to give a half-day off at each our our factories in Delhi, Düsseldorf, and Detroit. We would end out a memo saying all factories will close at 2020-01-23T12:00:00. This means noon in each locality, with noon arriving much earlier in Delhi first, then hours later in Düsseldorf, and lastly arriving several hours later in Detroit.
  • We are booking appointments. Politicians around the world have shown a surprising propensity for changing the offset of their respective time zone(s). So if that next dental appointment is meant to be at 3 PM on the clock after accounting for any such change, then book an appointment with a LocalDateTime and a ZoneId, each stored separately. When building a calendar schedule with specific points on the timeline, dynamically instantiate a ZonedDateTime by combining the two stored pieces.
  • When the time zone or offset is unknown. Frankly, I would consider this case inappropriate. If a zone or offset is intended but undetermined, you have bad data. That would be like storing a price of a product without knowing which currency was intended.

For more info, see this Question, What's the difference between Instant and LocalDateTime?.

OffsetDateTime

Instead you should be parsing as an OffsetDateTime.

You should be able to directly parse that string, as it complies with the ISO 8601 standard used by default in the java.time classes. Unfortunately the OffsetDateTime may still have a small bug in not being able to parse by default a value where the optional colon in the offset has been omitted. See this Question, Cannot parse String in ISO 8601 format, lacking colon in offset, to Java 8 Date.

So we need to specify a formatting pattern: "uuuu-MM-dd'T'HH:mm:ss.SSSX".

OffsetDateTime.parse( 
    "2018-02-13T10:20:12.120+0000" , 
    DateTimeFormatter.ofPattern( "uuuu-MM-dd'T'HH:mm:ss.SSSX" )
)

Table of types of date-time class in modern java.time versus legacy.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154