The java.util.TimeZone
class is a built-in class in Java that represents a time zone. It is used to convert dates and times between local time and Coordinated Universal Time (UTC), as well as to determine the rules for daylight saving time in a particular time zone.
The TimeZone
class provides several methods to manipulate time zones, including:
getDefault()
: Returns the default time zone for the current JVM.getTimeZone(String ID)
: Returns aTimeZone
object for the given time zone ID.getDisplayName(boolean daylightTime, int style)
: Returns the display name of this time zone.getOffset(long date)
: Returns the offset of this time zone from UTC for the given date and time.
Here’s an example of using the TimeZone
class to get the current time zone and to display the current date and time in that time zone:
import java.util.Calendar; import java.util.TimeZone; public class TimeZoneExample { public static void main(String[] args) { // Get the default time zone TimeZone timeZone = TimeZone.getDefault(); // Get a calendar instance for the current date and time Calendar calendar = Calendar.getInstance(); // Set the time zone for the calendar calendar.setTimeZone(timeZone); // Display the current date and time in the current time zone System.out.println("Current time zone: " + timeZone.getDisplayName()); System.out.println("Current date and time: " + calendar.getTime()); } }
Output:
Current time zone: Pacific Standard Time Current date and time: Fri May 07 17:34:10 PDT 2023
In this example, we first get the default time zone using the getDefault()
method. We then create a Calendar
instance using the getInstance()
method, and set its time zone to the default time zone using the setTimeZone()
method. Finally, we display the current time zone and the current date and time in that time zone using the getDisplayName()
and getTime()
methods, respectively.
Java TimeZone class declaration:
The java.util.TimeZone
class is declared as follows:
public abstract class TimeZone extends Object implements Serializable, Cloneable
Here’s a brief explanation of each part of the class declaration:
public
: TheTimeZone
class is a public class that can be accessed from anywhere.abstract
: TheTimeZone
class is an abstract class, which means that it cannot be instantiated directly. Instead, you must use one of its concrete subclasses, such asSimpleTimeZone
.class
: TheTimeZone
keyword indicates that this is a class definition.extends Object
: TheTimeZone
class inherits from theObject
class, which is the root class of all Java classes.implements Serializable, Cloneable
: TheTimeZone
class implements theSerializable
andCloneable
interfaces, which means that instances of theTimeZone
class can be serialized and cloned.Serializable
: TheSerializable
interface allows an object to be converted into a byte stream and then restored at a later time.Cloneable
: TheCloneable
interface allows an object to be cloned, which means that a new object is created that is an exact copy of the original object.
In summary, the TimeZone
class is an abstract class that represents a time zone in Java. It provides a set of methods to manipulate time zones, such as converting dates and times between local time and Coordinated Universal Time (UTC), and determining the rules for daylight saving time in a particular time zone.
Methods of Java TimeZone:
The java.util.TimeZone
class provides a number of methods to manipulate time zones. Here’s an overview of some of the most commonly used methods:
getDefault()
: Returns the defaultTimeZone
for the current JVM.
public static synchronized TimeZone getDefault()
getTimeZone(String ID)
: Returns aTimeZone
object for the given time zone ID. The ID is a string that represents a time zone, such as “America/Los_Angeles” or “GMT”.
public static synchronized TimeZone getTimeZone(String ID)
getID()
: Returns the ID of thisTimeZone
object.
public String getID()
getDisplayName()
: Returns the display name of thisTimeZone
object.
public final String getDisplayName()
getOffset(long date)
: Returns the offset of thisTimeZone
object from UTC for the given date and time.
public abstract int getOffset(long date)
inDaylightTime(Date date)
: Returnstrue
if the specifiedDate
is in daylight saving time in thisTimeZone
object,false
otherwise.
public abstract boolean inDaylightTime(Date date)
useDaylightTime()
: Returnstrue
if thisTimeZone
object observes daylight saving time,false
otherwise.
public abstract boolean useDaylightTime()
These methods are just a few of the many methods provided by the TimeZone
class. By using these methods, you can work with time zones and manipulate dates and times in different time zones as needed.
Java TimeZone class Example: getAvailableIDs()
The java.util.TimeZone
class provides the getAvailableIDs()
method, which returns an array of all the available time zone IDs. You can use this method to get a list of all the time zones that are supported by your Java runtime environment. Here’s an example that demonstrates how to use this method:
import java.util.Arrays; import java.util.TimeZone; public class TimeZoneExample { public static void main(String[] args) { // Get all available time zone IDs String[] timeZoneIDs = TimeZone.getAvailableIDs(); // Sort the array Arrays.sort(timeZoneIDs); // Display the time zone IDs System.out.println("Available time zone IDs:"); for (String id : timeZoneIDs) { System.out.println(id); } } } import java.util.Arrays; import java.util.TimeZone; public class TimeZoneExample { public static void main(String[] args) { // Get all available time zone IDs String[] timeZoneIDs = TimeZone.getAvailableIDs(); // Sort the array Arrays.sort(timeZoneIDs); // Display the time zone IDs System.out.println("Available time zone IDs:"); for (String id : timeZoneIDs) { System.out.println(id); } } }
Output:
Available time zone IDs: Africa/Abidjan Africa/Accra Africa/Addis_Ababa Africa/Algiers Africa/Asmara Africa/Asmera ...
In this example, we first call the getAvailableIDs()
method to get an array of all the available time zone IDs. We then sort the array using the Arrays.sort()
method. Finally, we loop through the array and display each time zone ID using a for
loop.
By using the getAvailableIDs()
method, you can get a list of all the time zones that are supported by your Java runtime environment, which can be useful for building user interfaces that allow users to select a time zone, or for performing time zone conversions in your Java applications.
Java TimeZone class Example: getOffset()
The java.util.TimeZone
class provides the getOffset(long date)
method, which returns the offset of a time zone from UTC for a given date and time. Here’s an example that demonstrates how to use this method:
import java.util.Calendar; import java.util.TimeZone; public class TimeZoneExample { public static void main(String[] args) { // Get the current date and time Calendar calendar = Calendar.getInstance(); long date = calendar.getTimeInMillis(); // Get the time zone object for Los Angeles TimeZone timeZone = TimeZone.getTimeZone("America/Los_Angeles"); // Get the offset for Los Angeles for the current date and time int offset = timeZone.getOffset(date); // Display the offset in hours and minutes System.out.printf("UTC offset for Los Angeles on %tF at %tT: %d hours, %d minutes%n", calendar, calendar, offset / 3600000, (offset / 60000) % 60); } }
Output:
UTC offset for Los Angeles on 2023-05-07 at 13:32:49: -7 hours, 0 minutes
In this example, we first get the current date and time using the Calendar.getInstance()
method. We then get the TimeZone
object for Los Angeles using the TimeZone.getTimeZone("America/Los_Angeles")
method. Finally, we call the getOffset(date)
method on the TimeZone
object to get the offset for Los Angeles for the current date and time.
The offset is returned in milliseconds, so we divide it by 3600000 to get the number of hours and by 60000 to get the number of minutes. We then use printf()
to display the offset in hours and minutes.
By using the getOffset()
method, you can determine the UTC offset for a particular time zone and date, which can be useful for performing time zone conversions or displaying dates and times in different time zones.
Java TimeZone class Example: getID()
The java.util.TimeZone
class provides the getID()
method, which returns the ID of a TimeZone
object. Here’s an example that demonstrates how to use this method:
import java.util.TimeZone; public class TimeZoneExample { public static void main(String[] args) { // Get the default time zone for this JVM TimeZone defaultTimeZone = TimeZone.getDefault(); // Get the ID of the default time zone String timeZoneID = defaultTimeZone.getID(); // Display the ID of the default time zone System.out.println("Default time zone ID: " + timeZoneID); } }
Output:
Default time zone ID: America/Los_Angeles
In this example, we first get the default time zone for the current JVM using the TimeZone.getDefault()
method. We then call the getID()
method on the TimeZone
object to get the ID of the default time zone. Finally, we use println()
to display the ID of the default time zone.
By using the getID()
method, you can determine the ID of a TimeZone
object, which can be useful for performing time zone conversions or displaying dates and times in different time zones.
Java TimeZone class Example: getDisplayName()
The java.util.TimeZone
class provides the getDisplayName()
method, which returns a string that represents the display name of a TimeZone
object. Here’s an example that demonstrates how to use this method:
import java.util.TimeZone; public class TimeZoneExample { public static void main(String[] args) { // Get the default time zone for this JVM TimeZone defaultTimeZone = TimeZone.getDefault(); // Get the display name for the default time zone String displayName = defaultTimeZone.getDisplayName(); // Display the display name of the default time zone System.out.println("Default time zone display name: " + displayName); } }
Output:
Default time zone display name: Pacific Standard Time
In this example, we first get the default time zone for the current JVM using the TimeZone.getDefault()
method. We then call the getDisplayName()
method on the TimeZone
object to get the display name of the default time zone. Finally, we use println()
to display the display name of the default time zone.
By using the getDisplayName()
method, you can determine the display name of a TimeZone
object, which can be useful for displaying dates and times in different time zones in a user-friendly way.
Java TimeZone class Example: getDefault()
The java.util.TimeZone
class provides the getDefault()
method, which returns the default TimeZone
for this JVM. Here’s an example that demonstrates how to use this method:
import java.util.TimeZone; public class TimeZoneExample { public static void main(String[] args) { // Get the default time zone for this JVM TimeZone defaultTimeZone = TimeZone.getDefault(); // Display the ID and display name of the default time zone System.out.println("Default time zone ID: " + defaultTimeZone.getID()); System.out.println("Default time zone display name: " + defaultTimeZone.getDisplayName()); } }
Output:
Default time zone ID: America/Los_Angeles Default time zone display name: Pacific Standard Time
In this example, we first call the getDefault()
method on the TimeZone
class to get the default TimeZone
for this JVM. We then call the getID()
and getDisplayName()
methods on the TimeZone
object to get the ID and display name of the default time zone. Finally, we use println()
to display the ID and display name of the default time zone.
By using the getDefault()
method, you can determine the default TimeZone
for this JVM, which can be useful for performing time zone conversions or displaying dates and times in different time zones.
Java TimeZone class Example: setID()
The java.util.TimeZone
class provides the setID()
method, which sets the ID of a TimeZone
object. Here’s an example that demonstrates how to use this method:
import java.util.TimeZone; public class TimeZoneExample { public static void main(String[] args) { // Get the default time zone for this JVM TimeZone defaultTimeZone = TimeZone.getDefault(); // Display the ID of the default time zone System.out.println("Default time zone ID before setting: " + defaultTimeZone.getID()); // Set the ID of the default time zone defaultTimeZone.setID("Europe/London"); // Display the ID of the default time zone after setting System.out.println("Default time zone ID after setting: " + defaultTimeZone.getID()); } }
Output:
Default time zone ID before setting: America/Los_Angeles Default time zone ID after setting: Europe/London
In this example, we first get the default time zone for the current JVM using the TimeZone.getDefault()
method. We then call the getID()
method on the TimeZone
object to get the ID of the default time zone before setting it to a new value. We use println()
to display the ID of the default time zone before setting it. Then, we call the setID()
method on the TimeZone
object to set the ID of the default time zone to “Europe/London”. Finally, we use println()
again to display the ID of the default time zone after setting it to the new value.
By using the setID()
method, you can set the ID of a TimeZone
object to a new value, which can be useful for performing time zone conversions or displaying dates and times in different time zones.