Java Cookbook
timezone
Problem:
You want to get a localized display name for a time zone.
Solution:
You can get a display name for a TimeZone in Java very easily by specifying a locale in the getDisplayName method.
To get a display name for Canada/Mountain in Japanese:
Problem:
You know what your offset from UTC is and you want to know what timezones are available for that offset
Solution:
The Java TimeZone class uses string ids to retrieve an instance. You can get a list of all TimeZone ids, but the list is quite large. Luckily you can narrow the results by specifying an offset from UTC and retrieve only the results with that raw offset.
The offset is specified as milliseconds from UTC. Go figure.
Problem:
You want to know what time zones are available on your system.
Solution:
Java uses the TimeZone class to handle date/time casting to and from UTC. A TimeZone is retrieved by specifying a string id. Most of these ids are Olsen time zone ids. You can retrieve an array of time zone ids by calling the static getAvailableIDs() method.
To get all available IDs:
Problem:
You want to format a date and cast it to a timezone.
Solution:
The Java DateFormat class can cast a date to a new time zone at the same time as formatting it. This can be accomplished simply by specifying a TimeZone on the DateFormat object.
To format and cast a date to PST:
If you are testing any of these recipes in Eclipse and the characters are not displaying correctly in your console visit http://i18ncookbook.com/eclipse_settings.
This site is ad supported. I hope you find something among our sponsors worth clicking. ;)
i18n search