Table of Contents
Methods of TimeZone Class in Java
Conclusion
Home Java javaTutorial Java TimeZone

Java TimeZone

Aug 30, 2024 pm 03:53 PM
java

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

Java Program to Find the Volume of Capsule Java Program to Find the Volume of Capsule Feb 07, 2025 am 11:37 AM

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

PHP vs. Python: Core Features and Functionality PHP vs. Python: Core Features and Functionality Apr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

Create the Future: Java Programming for Absolute Beginners Create the Future: Java Programming for Absolute Beginners Oct 13, 2024 pm 01:32 PM

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.

PHP: The Foundation of Many Websites PHP: The Foundation of Many Websites Apr 13, 2025 am 12:07 AM

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

See all articles