roark
roark

Reputation: 830

Convert UTC offset to USA time zone with Joda Time

Given a UTC offset, i.e. -5, can I determine the USA time zone using Joda Time? One of Pacific, Mountain, Central, Easter is sufficient. I can't use Java 8.

Upvotes: 1

Views: 251

Answers (2)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 78945

You can always obtain a timezone offset from a timezone ID but the reverse of it is not possible because the same timezone offset can be applicable to many timezone IDs. Check List of tz database time zones to find many examples of it.

Note that some timezone IDs can have different timezone offsets (e.g. because of DST or when a country decides to change it). However, you do not need to worry about it as long as the timezone database of your Java installation is up-to-date.

ZonedDateTime has been designed to adjust the timezone offset automatically according to the moment you use it for.

Demo:

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class Main {
    public static void main(String[] args) {
        LocalTime time = LocalTime.of(10, 20);
        ZoneId zoneId = ZoneId.of("Europe/London");

        System.out.println(ZonedDateTime.of(LocalDate.of(2021, Month.MARCH, 27), time, zoneId));
        System.out.println(ZonedDateTime.of(LocalDate.of(2021, Month.MARCH, 28), time, zoneId));

        // ############# Obtaining timezone offset from ZoneId #############
        LocalDateTime ldt = LocalDateTime.of(LocalDate.of(2021, Month.MARCH, 28), time);

        System.out.println(ZonedDateTime.of(ldt, zoneId).getOffset());
        // Alternatively
        System.out.println(zoneId.getRules().getOffset(ldt));
    }
}

Output:

2021-03-27T10:20Z[Europe/London]
2021-03-28T10:20+01:00[Europe/London]
+01:00
+01:00

ONLINE DEMO

The Z in the output is the timezone designator for zero-timezone offset. It stands for Zulu and specifies the Etc/UTC timezone (which has the timezone offset of +00:00 hours).

Learn more about the modern Date-Time API from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1499770

If you only have an offset, then no, you can't. -5 could be Central Daylight Time or Eastern Standard Time, for example. Likewise an offset of -7 could be Pacific Daylight Time or Mountain Standard Time.

If you have an offset and a date/time at which it was valid, you could make a pretty good guess - but only if you assume that everywhere in the US switches to daylight saving, and that everywhere does it at the same time - but that's not entirely the case.

Upvotes: 6

Related Questions