Home > Java > javaTutorial > body text

Java TimeZone

PHPz
Release: 2024-08-30 15:53:26
Original
612 people have browsed it

It is a direct subclass of java.lang.Object class present in java.util package that helps to represent the time zone offset of a particular region where the program is running, or the timezone being defined as argument and also helps to save other changes such as daylight savings; it implements Serializable and cloneable interfaces that make it compatible for network transfer as well as for cloning the setting of one-time zone object into another.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Methods of TimeZone Class in Java

This class uses the below 5 parameters to store time zone information:

  • public static final int SHORT = 1;
  • public static final int LONG= 1:
  • private static final int ONE_MINUTE = 60*1000;
  • private static final int ONE_HOUR = 60*ONE_MINUTE;
  • private static final int ONE_DAY = 24*ONE_HOUR;

Below are the methods available in this class:

1. public abstract int getOffset(int era, int year, int month, int day, int dayOfWeek, int milliseconds)

This method helps to retrieve the offset of the timezone in milliseconds that can be added to GMT to get the local time for a particular date specified in the arguments. Here 0 in month argument means January.

Code:

package Try;
import java.util.TimeZone;
publicclass Office
{
publicstaticvoid main(String[] args)
{
TimeZone obj     = TimeZone.getTimeZone("Africa/Bangui");
System.out.println("Offset value at 2020,5,6 date is  = " + obj.getOffset(1, 2020, 2, 5, 6, 2000));
}
}
Copy after login

Output:

Java TimeZone

2. abstract public void setRawOffset(int offsetMillis)

This method is used to set the time zone base to GMT that is used to get the local time by adding it to the UTC.

3. public abstract int getRawOffset()

It is used to get the amount of milliseconds independent of the daylight saving time and added to UTC to retrieve the standard time zone.

Code:

import java.util.TimeZone;
publicclass Office
{
publicstaticvoid main(String[] args)
{
TimeZone obj     = TimeZone.getTimeZone("Pacific/Pago_Pago");
System.out.println("RawOffset in the start is = " + obj.getRawOffset());
obj.setRawOffset(7000000);
System.out.println("RawOffset "+ "set to 7000000");
System.out.println("RawOffset after changes is = "          + obj.getRawOffset());
Copy after login

}
}

Output:

Java TimeZone

4. public boolean observesDaylightTime()

This method is used to check if the timezone is currently in daylight saving time or any transition is scheduled for future schedule changes and send true, in that case otherwise false.

Code:

import java.util.*;
publicclass Office
{
publicstaticvoid main(String[] args)
{
TimeZone obj     = TimeZone.getTimeZone("Europe/Rome");
System.out.println(obj.getID()+" is in Daylight or will be in future transitions = " + obj.observesDaylightTime());
}
}
Copy after login

Output:

Java TimeZone

5. static String[] getAvailableIDs()

It is used to get the array of all the supported and available IDs under that timezone in string format.

Code:

import java.util.*;
publicclass Office
{
publicstaticvoid main(String[] args)
{
String[] idArray= TimeZone.getAvailableIDs();
System.out.println("Available IDs are \n");
for(String a :idArray){
System.out.print(a);
System.out.print("\n");
}
}
}
Copy after login

Output:

Java TimeZone

6. public String getDisplayName(boolean daylight, int style, Locale locale)

This method is used to get the name of the time zone used. In case the daylight argument is true, the daylight saving time zone is returned; otherwise, the standard time zone is returned. Style argument specifies if LONG name is required or SHORT name and in the locale specified. In case the style given is invalid, IllegalArgumentException is thrown, and NullPointerException is thrown in case locale argument is not specified.

7. public static TimeZone getDefault()

This method helps us to retrieve the default value for the Time Zone for a particular region.

8. public StringgetID()

It is used to retrieve the ID of this time zone.

Example program explaining above three methods:

Code:

import java.util.TimeZone;
publicclass Office
{
publicstaticvoid main(String[] args)
{
TimeZone obj1    = TimeZone.getDefault();
System.out.println("Display Name of default Offset is = " + obj1.getDisplayName(false,0));
System.out.println("Id of Default Offset = " + obj1.getID());
}
}
Copy after login

Output:

Java TimeZone

9. public abstract boolean inDaylightTime(Date date)

This method is used to query if the particular date given in the argument is in Daylight saving Time in that particular timezone and returns true otherwise false.

Code:

import java.util.*;
publicclass Office
{
publicstaticvoid main(String[] args)
{
TimeZone obj     = TimeZone.getTimeZone("Europe/Rome");
Date dt = new Date();
System.out.println(dt.toString()+" is in Daylight Savings or not = " + obj.inDaylightTime(dt));
}
}
Copy after login

Output:

Java TimeZone

10. public abstract boolean useDaylightTime()

This method is used to verify if the given time zone supports Daylight Savings Time schedule changes; if yes, it returns true otherwise false.

Code:

import java.util.*;
publicclass Office
{
publicstaticvoid main(String[] args)
{
TimeZone obj     = TimeZone.getTimeZone("Europe/Rome");
System.out.println(obj.getID()+" supports Daylight Savings or not = \n" + obj.useDaylightTime());
}
}
Copy after login

Output:

Java TimeZone

11. public Boolean has SameRules(TimeZone tz1)

This method helps to analyze if the calling time Zone object has the same rules as the time zone object passed in its argument and returns true otherwise false.

Code:

import java.util.*;
publicclass Office
{
publicstaticvoid main(String[] args)
{
TimeZone obj     = TimeZone.getTimeZone("Africa/Bangui");
TimeZone obj1     = TimeZone.getTimeZone("Europe/Rome");
System.out.println(obj.getID() + " has Same Rules as of " +obj.getID() + " = " +obj.hasSameRules(obj));
System.out.println(obj.getID() + " has Same Rules as of " +obj1.getID() + " = " +obj.hasSameRules(obj));
}
}
Copy after login

Output:

Java TimeZone

Conclusion

It is a serializable and cloneable class present in java.util package and is a direct sub class of java.lang.object class which helps to represent the time zone offset of a particular region in the 2 static final and int fields long and short and support various operations using the supported methods in them.

The above is the detailed content of Java TimeZone. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!