I got a timestamp in the following format:
2017-09-27T16:19:24+0000 How do I know which timezone that is? What's the DateTimeFormatter if I'm using Java 8?
4 Answers
ZonedDateTime
As you stated using Java 8, you can leverage ZonedDateTime by using
ZonedDateTime zdt = ZonedDateTime.parse("2017-09-27T16:19:24+0000", DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ") Parsing rules are explained in DateTimeFormatter documentation. It is not exactly the ISO 8601 ISO_OFFSET_DATE_TIME as the offset should have been written +00:00 instead of +0000
Time zone vs time offset
Then, you can get the offset information with zdt.getZone(). However, you'll only get the Offset ID:
- Z - for UTC (ISO-8601)
- +hh:mm or -hh:mm - if the seconds are zero (ISO-8601)
- +hh:mm:ss or -hh:mm:ss - if the seconds are non-zero (not ISO-8601)
As one comment said, be careful that time offset is not time zone: A given time zone (e.g. time in France) does not have the same offset the whole year (summer time vs winter time).
1The timestamp given has a timezone offset (+0000), which represents +00 hours and +00 minutes from GMT+00.
This timezone pattern can be represented by the character Z for both SimpleDateFormat and DateTimeFormatter's ofPattern method.
The timezone you are handling can be represented by a pattern of yyyy-MM-dd'T'HH:mm:ssZ:
yyyyrepresents the current yearMMrepresents the month of the current yearddrepresents the current day of the current month'T'represents a quotedTcharacterHHrepresents the current hour of the current daymmrepresents the current minute of the current hourssrepresents the current second of the current minuteZrepresents the timezone offset from GMT
It looks like ISO 8601 format: dateTime±hhmm. Here hhmm is offset from UTC
The representation 2017-09-27T16:19:24+0000 gives +0000 so baseline UTC.
Timestamps themselves and LocalDateTime wrap a long count of seconds and do not contain a separate time zone info.
Java provides a class that maintains an addition time zone.
ZonedDateTime dt = LocalDateTime.now().atZone(ZoneId.of("Europe/Sofia")); One needs to be sure that the time was stored as UTC, +0000: a recommendation only.