jrharshath
jrharshath

Reputation: 26583

How to create DateTimeZone object from time zone string?

Is there a way to create a (joda) DateTimeZone object from a time zone string representation, like "EDT", or "+0330"?

Upvotes: 0

Views: 6785

Answers (3)

Andy Turner
Andy Turner

Reputation: 140494

It's perhaps worth pointing out that, whilst jodatime does not support three-letter time zone identifiers (aside from "UTC"), java.util.TimeZone does; and you can create a DateTimeZone instance from a TimeZone, using the DateTimeZone.forTimeZone method:

DateTimeZone.forTimeZone(TimeZone.getTimeZone("..."))

BUT TimeZone doesn't support "EDT" either. From the Javadoc:

For compatibility with JDK 1.1.x, some other three-letter time zone IDs (such as "PST", "CTT", "AST") are also supported. However, their use is deprecated...

Note the "some": at the time of writing, PDT is not listed in TimeZone.getAvailableIDs() (on ideone.com, at least). If you try to use "PDT" here, you will end up with the GMT timezone:

[TimeZone.getTimeZone returns] the specified TimeZone, or the GMT zone if the given ID cannot be understood.

So this approach is best avoided, unless you can guarantee that you will only ever try to use it for supported time zone identifiers.

Upvotes: 6

Stephen C
Stephen C

Reputation: 719249

The Joda APIs do not support mapping of abbreviated timezone names (like "EDT").

Why?

Because they are ambiguous! For instance EDT can mean either UTC-4 or UTC+11.

So if you want to implement a mapping, you need to decide how you want the names to be mapped, and then create and populate a map yourself.

Source: http://www.timeanddate.com/library/abbreviations/timezones/

Upvotes: 1

Kumar Shorav
Kumar Shorav

Reputation: 531

You can use this code for getting DateTimeZone object -

//User Defined
        DateTimeZone dtz = DateTimeZone.forID("America/New_York");
        //System Default TimeZone
        DateTimeZone dtzz = DateTimeZone.forID(TimeZone.getDefault().getID());

Upvotes: 1

Related Questions