Search code examples
javajava-8java-timetemporal

How to get TimeZone from Temporal?


interface SomeDataClass {
  TemporalAccessor getSomeTime();
}

//somewhere in the impl...
public TemporalAccessor getSomeTime() {
  return OffsetDateTime.from(dateTimeFormatter.parse(someDateInstring));
}

Does anyone know how to get timezone data from this TemporalAccessor interface?


Solution

  • If you need the ZoneOffset or ZoneId, you can create it from the TemporalAccessor:

    TemporalAccessor acc = ZonedDateTime.now();
    System.out.println(ZoneOffset.from(acc));
    System.out.println(ZoneId.from(acc));