Java offers a built-in TimeZone class that belongs to the “java.util” package. This class provides several methods that help us get timezone information. Using these methods, we can get the current timezone, all available time zones, or a specific timezone. Also, we can set or modify a timezone. Don’t know how to get/set a timezone in Java? No worries, in this tutorial, we’ll provide stepwise instructions to get a time zone in Java.
How to Get the Default Timezone in Java
The TimeZone class belongs to the “java.util” package and offers different methods to work with the timezones. One such method is getDefault() which retrieves the system’s default timezone, as demonstrated in the following example:
import java.util.TimeZone;
public class GetTimezoneJava {
public static void main(String args[]) {
TimeZone defaultTZ = TimeZone.getDefault();
System.out.println("The Default Timezone is ==> " + defaultTZ);
}
}
In the above code:
- First, we import the TimeZone class from the util package to work with the timezones.
- In the main() method, we invoke the getDefault() method of the TimeZone class to get the system’s default timezone.
- Finally, we print the retrieved timezone on the console using the println() method:
How to Get All Timezone IDs in Java
We can use the getAvailableIDs() method of the TimeZone class to get all available timezone IDs. Here is an example that retrieves all timezone IDs on the console:
import java.util.TimeZone;
public class GetTimezoneJava {
public static void main(String args[]) {
String[] getIDs = TimeZone.getAvailableIDs();
System.out.println("The Available Timezone IDs are ==>");
for (int i = 0; i < getIDs.length; i++) {
System.out.println(getIDs[i]);
}
}
}
In this code example,
- We invoke the getAvailableIDs() method and store its result in a string array “getIDs”.
- After this, we use a for loop to traverse the complete array, access each array element, and print it on the console:
This way, each available timezone is printed on the console, as shown below:
How to Get the IDs According to a Specific Timezone Offset in Java
We can also get a list of IDs according to a specific timezone offset. For this purpose, we need to pass the raw offset as an argument to the getAvailableIDs() method, as illustrated below:
import java.util.TimeZone;
public class GetTimezoneJava {
public static void main(String args[]) {
String[] givenID = TimeZone.getAvailableIDs(-21600000);
System.out.println("The Available Ids in the Given TimeZone are ==>");
for (int i = 0; i < givenID.length; i++) {
System.out.println(givenID[i]);
}
}
}
In this example,
- We specify a raw offset of “-21600000” to the getAvailableIDs() method that represents -6 hours from GMT.
- We assign the result of the getAvailableIDs() method to a string array “givenID”.
- Finally, we use a for loop to iterate the array and print each timezone ID on the console:
How to Get the Current Time in Another Timezone
We can also get the current time in any specific timezone (i.e., other than the default timezone). To do this, we need to pass the desired timezone in the getTimeZone() method. For example, the below code prints the current time in the system’s default timezone and a specific timezone, i.e., “America/Chicago”:
import java.util.*;
public class GetTimezoneJava {
public static void main(String args[]) {
Calendar currInstance = Calendar.getInstance();
System.out.println("Current Time in Default TimeZone is ==>");
System.out.println("Current Hour = " + currInstance.get(Calendar.HOUR_OF_DAY));
System.out.println("Current Minute = " + currInstance.get(Calendar.MINUTE));
System.out.println("Current Second = " + currInstance.get(Calendar.SECOND));
System.out.println("Current Millisecond = " + currInstance.get(Calendar.MILLISECOND));
System.out.println("Current Time in America/Chicago TimeZone is ==> ");
currInstance.setTimeZone(TimeZone.getTimeZone("America/Chicago"));
System.out.println("Current Hour = " + currInstance.get(Calendar.HOUR_OF_DAY));
System.out.println("Current Minute = " + currInstance.get(Calendar.MINUTE));
System.out.println("Current Second = " + currInstance.get(Calendar.SECOND));
System.out.println("Current Millisecond = " + currInstance.get(Calendar.MILLISECOND));
}
}
In this example,
- First, we import the necessary classes like “Calendar” and “TimeZone” from the Java util package.
- In the main() method, we invoke the getInstance() method to get the current date and time (according to the system’s default timezone).
- Next, we use the get() method with the “HOUR_OF_DAY”, “MINUTE”, “SECOND”, and “MILLISECONDS” fields to get the current hour, minutes, seconds, and milliseconds, respectively.
- Next, we use the getTimeZone() with the “America/Chicago” as its argument to get the current time according to the “America/Chicago” timezone.
- Finally, we use the get() method to retrieve the current hour, minutes, seconds, and milliseconds according to the “America/Chicago” timezone:
How to Set the Base Timezone Offset to GMT in Java
We can use the setRawOffset() method to set the base timezone offset to any specific value relative to GMT. In the following example, we set the raw offset to 8 minutes:
import java.util.TimeZone;
public class GetTimezoneJava {
public static void main(String args[]) {
TimeZone givenTZ = TimeZone.getTimeZone("America/Chicago");
System.out.println("Getting RawOffset of Given Timezone ==>" + givenTZ.getRawOffset());
givenTZ.setRawOffset(800000);
System.out.println("The Current RawOffset Value is ==>" + givenTZ.getRawOffset());
}
}
In this code,
- First, we use the getTimeZone() method with the ID “America/Chicago”.
- Next, we get and print the raw offset of the “America/Chicago” timezone using the getRawOffset() and println() methods, respectively.
- After this, we invoke the setRawOffset() method to set the raw offset of the “givenTZ” time zone to 8 minutes.
- Finally, we print the updated raw offset value of the “America/Chicago” time zone on the console:
How to Get a Formatted Timezone in Java
We use the SimpleDateFormat class to format a date, time, or timezone. For instance, in the following code, we get different DateTime fields in a formatted manner:
import java.util.*;
import java.text.*;
public class GetTimezoneJava {
public static void main(String args[]){
Calendar currInstance = Calendar.getInstance();
SimpleDateFormat simpleformat = new SimpleDateFormat("yyyy-MM-dd hh:mm:s z");
System.out.println("The Current Instance is ==> " + simpleformat.format(currInstance.getTime()));
Format formatter = new SimpleDateFormat("YYYY");
String getYear = formatter.format(new Date());
System.out.println("The Current Year is ==> " + getYear);
formatter = new SimpleDateFormat("MM");
String getMonth = formatter.format(new Date());
System.out.println("The Current Month is ==> " + getMonth);
formatter = new SimpleDateFormat("dd");
String getDay = formatter.format(new Date());
System.out.println("The Current Day is ==> " + getDay);
formatter = new SimpleDateFormat("H");
String getHour = formatter.format(new Date());
System.out.println("The Current Hour is ==> " + getHour);
formatter = new SimpleDateFormat("mm");
String getMinute = formatter.format(new Date());
System.out.println("Current Minutes ==> " + getMinute);
formatter = new SimpleDateFormat("ss");
String getSeconds = formatter.format(new Date());
System.out.println("Current Seconds ==> " + getSeconds);
formatter = new SimpleDateFormat("a");
String getMarker = formatter.format(new Date());
System.out.println("AM or PM ? " + getMarker);
formatter = new SimpleDateFormat("z");
String getTimeZone = formatter.format(new Date());
System.out.println("The Current TimeZone is ==> " + getTimeZone);
}
}
In this code,
- We use the format() method to get the current DateTime and timezone in a specific format.
- Next, we invoke the SimpleDateFormat() constructor multiple times with the “YYYY”, “MM”, “dd”, “H”, “mm”, “ss”, “a”, and “z” parameters, respectively.
- As a result, we get the current year, month, day, hour minutes, seconds, AM or PM marker, and “timezone”, respectively:
That’s all about getting a Timezone in Java.
Conclusion
To get a timezone in Java, we can invoke the getDefault() method of the Timezone class. This method retrieves the default timezone based on the system’s timezone setting. To get a specific timezone, we must specify the timezone ID in the getDefault() method. Also, we can get all available timezones by invoking the getAvailableIDs() method of the TimeZone class. In this Java guide, we covered different aspects of timezones, such as getting a timezone, setting a timezone, formatting a timezone, etc.