Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timezone format, how to know the timezone

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?

like image 914
musicsquad Avatar asked Sep 18 '25 21:09

musicsquad


1 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).

like image 155
Al-un Avatar answered Sep 20 '25 10:09

Al-un