The Java Developers Almanac 1.4


Order this book from Amazon.

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

e370. Getting the Current Time in Another Time Zone

    // Get the current time in Hong Kong
    Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("Hongkong"));
    
    int hour12 = cal.get(Calendar.HOUR);         // 0..11
    int minutes = cal.get(Calendar.MINUTE);      // 0..59
    int seconds = cal.get(Calendar.SECOND);      // 0..59
    boolean am = cal.get(Calendar.AM_PM) == Calendar.AM;
    
    // Get the current hour-of-day at GMT
    cal.setTimeZone(TimeZone.getTimeZone("GMT"));
    int hour24 = cal.get(Calendar.HOUR_OF_DAY);  // 0..23
    
    // Get the current local hour-of-day
    cal.setTimeZone(TimeZone.getDefault());
    hour24 = cal.get(Calendar.HOUR_OF_DAY);      // 0..23

 Related Examples
e369. Getting the Current Time
e371. Retrieving Information on All Available Time Zones
e372. Converting Times Between Time Zones

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


© 2002 Addison-Wesley.