简体   繁体   中英

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?

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 (eg time in France) does not have the same offset the whole year (summer time vs winter time).

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.

The 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 :

  • yyyy represents the current year
  • MM represents the month of the current year
  • dd represents the current day of the current month
  • 'T' represents a quoted T character
  • HH represents the current hour of the current day
  • mm represents the current minute of the current hour
  • ss represents the current second of the current minute
  • Z represents the timezone offset from GMT

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM