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" )
)
