The i18n Cookbook - recipies for a global society

  • java cookbook
  • about the author
Home › Java Internationalization Cookbook › Dates and Times › Formating dates and times

Java Cookbook

  • Java Internationalization Cookbook
    • Locales
    • Dates and Times
      • Calendars
      • Formating dates and times
        • Format a time amount
        • Format a time interval
        • Format and cast a date to a timezone
        • Get all time zone ids
        • Get an array of day names
        • Get an array of timezone ids for offset
        • Get the best date format pattern
        • Get the display name for a Timezone
        • Parse a formatted date string
    • Numerical Systems
    • Misc
    • Resource Bundles
    • Unicode, Transliteration, and Charactersets

Get an array of timezone ids for offset

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.

 

To get all TimeZone ids for an offset of -7:00 from UTC:

//Get all Timezone ids for an offset.
//For some reason the offset is in milliseconds. 
//To my knowledge there is no offset increment less than half an hour
//Here we get all ids for an offset of -7 hours from UTC
String[] ids = TimeZone.getAvailableIDs(-1000*60*60*7);
//Loop through ids and output
for(int x = 0; x < ids.length; x++){
    System.out.println(ids[x]);
}


 

Output:

America/Boise
America/Cambridge_Bay
America/Chihuahua
America/Dawson_Creek
America/Denver
America/Edmonton
America/Hermosillo
America/Inuvik
America/Mazatlan
America/Phoenix
America/Shiprock
America/Yellowknife
Canada/Mountain
Etc/GMT+7
MST
MST7MDT
Mexico/BajaSur
Navajo
PNT
SystemV/MST7
SystemV/MST7MDT
US/Arizona
US/Mountain

‹ Get an array of day names up Get the best date format pattern ›
  • timezone
  • Printer-friendly version
  • Add new comment

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

Google
Custom Search

Search

Tags in Tags

calendar date icu4j Java Locale number format numberformat parse spellout timezone transliteration transliterator
more tags

User login

  • Create new account
  • Request new password
  • java cookbook
  • about the author