The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > java.util  [50 examples] > Time  [4 examples]

e372. Converting Times Between Time Zones

There is a convenient setTimeZone() method in the Calendar object. However, it doesn't always return the correct results when used after a calendar field is set. This example demonstrates a more reliable way to convert a specific time from one time zone to another. It involves creating two Calendar instances and transfering the UTC (Coordinate Universal Time) from one to the other. The UTC is a representation of time and date that is independent of time zones.
    // Given a local time of 10am, get the time in Japan
    // Create a Calendar object with the local time zone
    Calendar local = new GregorianCalendar();
    local.set(Calendar.HOUR_OF_DAY, 10);               // 0..23
    local.set(Calendar.MINUTE, 0);
    local.set(Calendar.SECOND, 0);
    
    // Create an instance using Japan's time zone and set it with the local UTC
    Calendar japanCal = new GregorianCalendar(TimeZone.getTimeZone("Japan"));
    japanCal.setTimeInMillis(local.getTimeInMillis());
    
    // Get the foreign time
    int hour = japanCal.get(Calendar.HOUR);            // 3
    int minutes = japanCal.get(Calendar.MINUTE);       // 0
    int seconds = japanCal.get(Calendar.SECOND);       // 0
    boolean am = japanCal.get(Calendar.AM_PM) == Calendar.AM; //true
    
    
    // Given a time of 10am in Japan, get the local time
    japanCal = new GregorianCalendar(TimeZone.getTimeZone("Japan"));
    japanCal.set(Calendar.HOUR_OF_DAY, 10);            // 0..23
    japanCal.set(Calendar.MINUTE, 0);
    japanCal.set(Calendar.SECOND, 0);
    
    // Create a Calendar object with the local time zone and set
    // the UTC from japanCal
    local = new GregorianCalendar();
    local.setTimeInMillis(japanCal.getTimeInMillis());
    
    // Get the time in the local time zone
    hour = local.get(Calendar.HOUR);                   // 5
    minutes = local.get(Calendar.MINUTE);              // 0
    seconds = local.get(Calendar.SECOND);              // 0
    am = local.get(Calendar.AM_PM) == Calendar.AM;     // false

 Related Examples
e369. Getting the Current Time
e370. Getting the Current Time in Another Time Zone
e371. Retrieving Information on All Available Time Zones

See also: Arrays    Bits    Collections    Dates    Hash Tables    Lists    Property Files    Sets    Sorted Collections    Timers   


© 2002 Addison-Wesley.