Open In App

Java.util.TimeZone Class | Set 1

TimeZone class is used to represents a time zone offset, and also figures out daylight savings.
What is a Time Zone and Time Offset?
“Time Zone” is used to describe the current time for different areas of the world. It refers to one of the specific regions out of the 24 total regions in the world that are divided up by longitude. Within each one of those regions, a standard version of time is maintained. 
 

For Example : To find the time zone in hours of a particular location, you can take the longitude in degrees and divide it by 15. So, for example, 105° E would be 105/15 which equals 7. That translates to the time zone being 7 hours ahead of UTC or GMT time, which can also be labelled as UTC+7. Where 7 is a time offset for that location.
 



TimeZone Class in Java

Class declaration 



public abstract class TimeZone extends 
Object implements Serializable, Cloneable

Methods of TimeZone Class : 

Syntax : public static String[] getAvailableIDs()
Syntax : public static String[] getAvailableIDs(int rawOffset)
Parameters: rawOffset - the given time zone GMT offset in milliseconds.




// Java program for Demonstration of
// getAvailableIDs() and
// getAvailableIDs(int rawOffset ) methods
import java.util.TimeZone;
 
public class TimeZoneDemo {
 
    public static void main(String[] args)
    {
 
        // get all the  timezones ids defined by TimeZone class
        String[] availableTimezones = TimeZone.getAvailableIDs();
 
        // Print Total no of TimeZones
        System.out.println("Total No of Time Zone Available");
 
        System.out.println(availableTimezones.length);
 
        // get all the  timezones  whose offset is
        // 7200000 milliseconds means 2 hour
        String[] timezones = TimeZone.getAvailableIDs(7200000);
 
        // Print Total no of TimeZones
        System.out.println("No of Time Zone having time offset 2 hour");
 
        System.out.println(timezones.length);
 
        // print all timezones names
        System.out.println("Timezone names having time offset 2 hour");
 
        for (int i = 0; i < timezones.length; i++)
            System.out.println(timezones[i]);
    }
}

Output:  
Total No of Time Zone Available
628
No of Time Zone having a time offset 2 hour
43
Timezone names having a time offset 2 hour
ART
Africa/Blantyre
Africa/Bujumbura
Africa/Cairo......
............
Syntax : public static TimeZone getDefault()
Syntax : public final String getDisplayName() 




// Java program for Demonstration of
// getDefault() and getDisplayName() methods
import java.util.TimeZone;
 
public class TimeZoneDemo {
 
    public static void main(String[] args)
    {
 
        // Get your Local Time Zone Where this Program is Running.
        TimeZone timezone = TimeZone.getDefault();
 
        // Get the Name of Time Zone
        String LocalTimeZoneDisplayName = timezone.getDisplayName();
 
        // Print the Name of Time Zone
        System.out.println(LocalTimeZoneDisplayName);
    }
}

Output:  
Coordinated Universal Time
Syntax :public static TimeZone getTimeZone(String ID)
Parameters: ID - the ID for a TimeZone.
Syntax : public int getDSTSavings()
Syntax : public String getID()




// Java program for Demonstration of
// getTimeZone(String ID),
// getDSTSavings()  and getID() methods
import java.sql.Time;
import java.util.TimeZone;
 
public class TimeZoneDemo {
 
    public static void main(String[] args)
    {
 
        // creating Timezone object whose id is Europe/Berlin
        TimeZone timezone = TimeZone.getTimeZone("Europe/Berlin");
 
        // printing the Display Name of this timezone object
        System.out.println("Display Name");
 
        System.out.println(timezone.getDisplayName());
 
        // getting DST in milliseconds
        int timeInMilliseconds = timezone.getDSTSavings();
 
        System.out.println("\nDST of Europe/Berlin is");
        System.out.println(timezone.getDSTSavings());
 
        // get Id of your Default Time Zone
        TimeZone defaultTimezone = TimeZone.getDefault();
 
        System.out.println("\nThe id of default Time zone is");
        System.out.println(timezone.getID());
    }
}

Output: 
Display Name
Central European Time

DST of Europe/Berlin is
3600000

The id of default Time zone is
Europe/Berlin
Syntax : the method is used to return the offset of this time zone
 from UTC at the passed date in method.
Parameters: date - the date represented in milliseconds
 since January 1, 1970 00:00:00 GMT
Syntax :Syntax : public abstract boolean inDaylightTime(Date date)
Parameters:date - the given Date.
Syntax :public boolean observesDaylightTime()




// Java program for
// Demonstration of getOffset(long date),
// inDaylightTime(Date date)  and
// observesDaylightTime() methods
import java.sql.Time;
import java.util.*;
 
public class TimeZoneDemo {
 
    public static void main(String[] args)
    {
 
        // creating Timezone object whose id is Europe/Berlin
        TimeZone timezone = TimeZone.getTimeZone("Europe/Berlin");
 
        // printing offset value
        System.out.println("Offset value of Europe/Berlin:");
 
        System.out.println(timezone.getOffset(Calendar.ZONE_OFFSET));
 
        // create Date Object
        Date date = new Date(2017, 04, 16);
 
        // checking the date is in day light time of that Time Zone or not
        System.out.println("\nDate 16/04/2017 is in Day Light Time of");
 
        System.out.println("Timezone: timezone.getDisplayName()");
        System.out.println(timezone.inDaylightTime(date));
 
        // check this Time Zone observes Day Light Time or Not
        System.out.println("\nTimeZone name " + timezone.getDisplayName());
 
        System.out.println("Observes Day Light Time");
        System.out.println(timezone.observesDaylightTime());
    }
}

Output:
Offset value of Europe/Berlin:
3600000

Date 16/04/2017 is in Day Light Time of
Timezone: timezone.getDisplayName()
true

TimeZone name Central European Time
Observes Day Light Time
true
Syntax : public static void setDefault(TimeZone zone)
Parameters: zone - the new default time zone
Syntax :public void setID(String ID)
Parameters: ID - the new time zone ID.
Syntax : public Object clone()




// Java program for Demonstration of
// setDefault(TimeZone zone),
// setID(String ID)  and clone() methods
import java.util.*;
 
public class TimeZoneDemo {
 
    public static void main(String[] args)
    {
 
        // My previous Default Time Zone is
        TimeZone DefaultTimeZone = TimeZone.getDefault();
 
        System.out.println("Current Default TimeZone:");
        System.out.println(DefaultTimeZone.getDisplayName());
 
        // Setting  Europe/Berlin as your Default Time Zone
        TimeZone timezone = TimeZone.getTimeZone("Europe/Berlin");
 
        timezone.setDefault(timezone);
        TimeZone NewDefaultTimeZone = TimeZone.getDefault();
        System.out.println("\nNew Default TimeZone:");
        System.out.println(NewDefaultTimeZone.getDisplayName());
 
        // change Id Europe/Berlin to Eur/Ber
        timezone.setID("Eur/Ber");
 
        System.out.println("\nNew Id of Europe/Berlin is");
        System.out.println(timezone.getID());
 
        // create copy of a time zone
        System.out.println("\nOriginal TimeZone ID:");
 
        System.out.println(timezone.getID());
        TimeZone clonedTimezone = (TimeZone)timezone.clone();
        System.out.println("Cloned TimeZone ID:");
        System.out.println(clonedTimezone.getID());
    }
}

Output:
Current Default TimeZone:
India Standard Time

New Default TimeZone:
Central European Time

New Id of Europe/Berlin is
Eur/Ber

Original TimeZone ID:
Eur/Ber
Cloned TimeZone ID:
Eur/Ber

Example : Print the Date and Time for Any Given Input Time Zone Where Program is Running. 




// Java program to illustrate
// java.util.timezone class
import java.text.*;
import java.util.*;
 
public class TimeZoneDemo {
 
    public static void main(String[] args)
    {
        // Get your Local Time Zone Where this Program is Running.
        TimeZone timezone = TimeZone.getDefault();
 
        // Get the Name of Time Zone
        String LocalTimeZoneName = timezone.getDisplayName();
 
        // Initialize your Date Object and Date Format to represent your Date
        Date date = new Date();
        DateFormat dformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 
        // set your local time Zone to your Date Format time Zone
        dformat.setTimeZone(timezone);
 
        // Print Date and Time for your Time Zone
        System.out.println("Date and time of your Local Time Zone:");
        System.out.println(LocalTimeZoneName + ", " + dformat.format(date));
    }
}

 Output:  
Date and time of your Local Time Zone:
Coordinated Universal Time, 2018-04-17 07:36:19

Reference – Oracle Documentation


Article Tags :