Menu
Topics Index
...
`

TimeZoneDemo - Java Example Program


TimeZoneDemo
import java.util.*;

class TimeZoneDemo
{
    public static void main(String arg[])
    {
        Calendar now = Calendar.getInstance(); // LINE A
        TimeZone timeZone = TimeZone.getTimeZone("IST"); // LINE B
        System.out.println(timeZone.getDisplayName()); // LINE C
        timeZone = TimeZone.getTimeZone("GMT");
        now.setTimeZone(timeZone); // LINE D
        System.out.println(timeZone.getDisplayName());
    
    }
}
OUTPUT

India Standard Time
Greenwich Mean Time

DESCRIPTION

In the above program we have created a calendar instance at LINE A and a TimeZone instance with India Standard Time zone. At LINE C we dislplayed the name of the time zone using getDisplay method. At LINE D we set the time zone to Greenwich Mean Time and displayed it.

THINGS TO TRY
  • Replace LINE D with the below code and see the output.
    TimeZone.setTimeZone(timeZone);
    The above line throws a compilation error since setTimeZone(TimeZone) is undefined for the type TimeZone.

© meritcampus 2019

All Rights Reserved.

Open In App