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)); } }
Output:
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());
}
}
Output:
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()); } }
Output:
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"); } } }
Output:
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()); } }
Output:
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)); } }
Output:
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()); } }
Output:
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)); } }
Output:
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Notepad++7.3.1
Easy-to-use and free code editor

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6
Visual web development tools