Java.util.TimeZone Class | Set 1
Last Updated :
03 Dec, 2021
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.
- The different time zones are calculated based on their relation to the coordinated universal time or UTC.
- A time offset is an amount of time subtracted from or added to Universal Time) time to get the current civil time, whether it is standard time or daylight-saving time (DST).
- We divide the whole earth east to west into 24 different regions based on longitude so each region is 15 degrees wider. So, there are 24 different Time zones available on earth. Each time zone is 15 degrees wide and there's a one-hour difference between each one.
- Depending on the distance east or west from the Greenwich Meridian you must either add or subtract the appropriate time for every 15-degree interval in Longitude.
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 :
- getAvailableIDs() - Using this method you can get all the available Time Zone IDs.
Syntax : public static String[] getAvailableIDs()
- getAvailableIDs (int rawOffset) - Using this method you can get an array of IDs, where the time zone for that ID has the specified GMT offset in milliseconds.
Syntax : public static String[] getAvailableIDs(int rawOffset)
Parameters: rawOffset - the given time zone GMT offset in milliseconds.
Java
// 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......
............
- getDefault() - Using this method you can get the TimeZone of place where Program is Running.
Syntax : public static TimeZone getDefault()
- getDisplayName() - Method returns a long standard time name of initialize TimeZone.
Syntax : public final String getDisplayName()
Java
// 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
- getTimeZone(String ID) - This method is used to get the TimeZone for the given ID.
Syntax :public static TimeZone getTimeZone(String ID)
Parameters: ID - the ID for a TimeZone.
- getDSTSavings() - Method returns the amount of time to be added to local standard time to get local wall clock time.
Syntax : public int getDSTSavings()
- getID() - This method is Used to Get the ID of this time zone.
Syntax : public String getID()
Java
// 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
- getOffset(long date) - The method is used to return the offset of this time zone from UTC at the passed date in method.
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
- inDaylightTime(Date date) - This method returns true if the given date is in Daylight Saving Time in this time zone else false.
Syntax :Syntax : public abstract boolean inDaylightTime(Date date)
Parameters:date - the given Date.
- observesDaylightTime() - This method returns true if this TimeZone is currently in Daylight Saving Time, or if a transition from Standard Time to Daylight Saving Time occurs at any future time.
Syntax :public boolean observesDaylightTime()
Java
// 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
- setDefault(TimeZone zone) - It is used to set the TimeZone that is returned by the getDefault method.
Syntax : public static void setDefault(TimeZone zone)
Parameters: zone - the new default time zone
- setID(String ID) - It is used to set the time zone ID.
Syntax :public void setID(String ID)
Parameters: ID - the new time zone ID.
- clone() - This method used to create copy of this TimeZone
Syntax : public Object clone()
Java
// 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
// 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
Similar Reads
java.time.ZoneId Class in Java
A ZoneId is used to identify the rules that used to convert between a LocalDateTime and an Instant of time. The actual rules, describing when and the way the offset changes, are defined by ZoneRules. This class is just an ID wont to obtain the underlying rules. The given approach has opted because t
3 min read
Java.util.TimeZone Class (Set-2) | Example On TimeZone Class
TimeZone class (the methods of this class was discussed in this article Java.util.TimeZone Class | Set 1) can be used in many cases like using TimeZone class we can get the time difference in term of hour and minute between two places.Problem : How we can get time difference of time in terms of hour
5 min read
java.time.ZonedDateTime Class in Java
ZonedDateTime is an immutable object representing a date-time along with the time zone. This class stores all date and time fields.This class stores time to a precision of nanoseconds and a time-zone, with a zone Offset used to handle local date-times. For example, the value â2nd October 2011 at 14:
8 min read
java.time.ZoneOffset Class in Java
A time-zone offset is that the amount of your time that a time-zone differs from Greenwich/UTC. This is often usually a hard and fast number of hours and minutes. From the time package of java, ZoneOffset class is employed to represent the fixed zone offset from UTC zone and inherits the ZoneId clas
3 min read
java.time.OffsetTime Class in Java
Java OffsetTime class is an immutable date-time object that represents a time, often viewed as hour-minute-second offset. OffsetTime class represents a time with an offset from UTC/Greenwich in the ISO-8601 calendar system, such as 18:30:45+08:00, often viewed as an hour-minute-second-offset. This c
7 min read
TimeZone clone() Method in Java with Examples
The clone() method of TimeZone class in Java is used to create an identical copy of an existing this TimeZone. Syntax: time_zone.clone() Parameters: The method does not take any parameters. Return Value: The method returns an instance of TimeZone which is the copy of this TimeZone. Below program ill
1 min read
java.time.OffsetDateTime Class in Java
Java is the most popular programming language and widely used programming language. Java is used in all kind of application like mobile application, desktop application, web application. In this Java java.time.OffsetDate, Time class is imported which is an immutable representation of a date-time wit
4 min read
TimeZone setID() Method in Java with Examples
The setID(String ID) method of TimeZone class in Java is used to set the time zone ID of this TimeZone. While this operation no other data of the time zone object is changed. Syntax: public void setID(String ID) Parameters: The method takes one parameter ID of String type which refers to the new ID
1 min read
java.time.LocalTime Class in Java
Java is the most popular programming language and widely used programming language. Java is used in all kinds of applications like mobile applications, desktop applications, web applications. As in Java, java.time.LocalTime class represents time, which is viewed as hour-minute-second. This class is
5 min read
Calendar setTimeZone() Method in Java with Examples
The setTimeZone(TimeZone time_zone) method in Calendar class takes a Time Zone value as a parameter and modifies or set the timezone represented by this Calendar. Syntax: public void setTimeZone(TimeZone time_zone) Parameters: The method takes one parameter time_zone of Date type and refers to the g
2 min read