ZoneId is a class in Java. time package, which has been introduced to specify the rules that are needed for conversion between Instant and LocalDateTime. This class is a subclass of ZoneOffset class and also serializable as it implements a Serializable interface. This class uses a particular format that is used to store the offset from UTC/Greenwich only. Being a value-based class, the use of identity-sensitive operations may lead to unpredictable results. This class represents most of the fixed offset IDs that use the same offset for all local date-times.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax of Java ZoneId
Following is a syntax of java zoneid:
Syntax:
public abstract class ZoneId extends Object implements Serializable
Field: public static final Map
This map is as follows:
- EST: -05:00
- HST: -10:00
- MST: -07:00
- ACT: Australia/Darwin
- AET: Australia/Sydney
- AGT: America/Argentina/Buenos_Aires
- ART: Africa/Cairo
- AST: America/Anchorage
- BET: America/Sao_Paulo
- BST: Asia/Dhaka
- CAT: Africa/Harare
- CNT: America/St_Johns
- CST: America/Chicago
- CTT: Asia/Shanghai
- EAT: Africa/Addis_Ababa
- ECT: Europe/Paris
- IET: America/Indiana/Indianapolis
- IST: Asia/Kolkata
- JST: Asia/Tokyo
- MIT: Pacific/Apia
- NET: Asia/Yerevan
- NST: Pacific/Auckland
- PLT: Asia/Karachi
- PNT: America/Phoenix
- PRT: America/Puerto_Rico
- PST: America/Los_Angeles
- SST: Pacific/Guadalcanal
- VST: Asia/Ho_Chi_Minh
Methods of Java ZoneId
Methods of java zoneid are given below:
1. public static ZoneId systemDefault()
This function is used to get the system default time- zone. This invokes TimeZone.getDefault() function to retrieve the value to default timeZone for that system, which means any updation made to system default Time-Zone will be reflected in the results. The output is converted to ZoneID format. Below 2 exceptions are thrown by this function must be handled:-
- DateTimeException: This indicates converted zone ID has an invalid format
- ZoneRulesException: This indicates converted zone region ID cannot be found
Code:
import java.time.ZoneId; public class Main { public static void main(String[] args) { ZoneId zone = ZoneId.systemDefault(); System.out.println("Output of systemDefault()-" +zone); } }
Output:
2. public static Set getAvailableZoneIds()
This function is used to return all available region-based IDs. The list is returned as a set of String. The string returned can be converted to the format of Zone-Id using Of(String) function. Offset-Ids are not included in the result of a set of Strings.
Code:
import java.time.ZoneId; import java.util.*; public class Main { public static void main(String[] args) { Set<string> zoneIds = ZoneId.getAvailableZoneIds(); List<string> zoneList = new ArrayList<string>(zoneIds); Collections.sort(zoneList); for (int i = 0; i <p><strong>Output:</strong></p> <p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/172500421161710.png?x-oss-process=image/resize,p_40" class="lazy" alt="Java ZoneId" ></p> <h4 id="public-static-ZoneId-of-String-zoneId">3. public static ZoneId of(String zoneId)</h4> <p>This function returns ZoneID or ZoneOffset for a valid ID passed as a string as an output to the function. ZoneOffset is returned based on the string’s starting character (if it is ‘Z’ or ‘+’ or ‘-‘). The ZoneID being returned always follow the ZoneRules. This conversion takes place using a complete parsing algorithm. In case the format of the argument is invalid, DateTimeException is thrown, and in the case of the region, ID can not be found, then ZoneRulesException is thrown.</p> <p><strong>Code:</strong></p> <pre class="brush:php;toolbar:false">import java.time.*; import java.util.*; import java.time.format.TextStyle; public class Main { public static void main(String[] args) { ZoneId zoneId = ZoneId.of("Asia/Calcutta"); System.out.println("Output of zoneid obtained using of function: "+ zoneId.normalized()); } }
Output:
4. public static ZoneId ofOffset(String prefix,ZoneOffset offset)
This method is used to obtain ZoneID when prefix and an offset are being passed as arguments. Zone ID with prefix and non zero Offset ID is returned. In this case, the prefix is “” ZoneOffset is returned.
The prefix must be ‘GMT’, ‘UTC’ or ‘UT’ or ‘’ otherwise, IllegalArgumentException is thrown by the method.
Code:
import java.time.*; import java.util.*; public class Main { public static void main(String[] args) { ZoneId zoneId = ZoneId.ofOffset("GMT", ZoneOffset.MAX); System.out.println("OfOffset on ZoneId: " + zoneId); } }
Ouput:
5. public static ZoneId from(TemporalAccessor temporal)
This method is used to convert a TemporalAccessor, an arbitrary set of date and time, object to an instance of ZoneId. This function works in a similar manner to TemporalQueries.zone() that extracts offset base zone id. Thus this method matches the signature of Temporal Query that is being used via ZoneId::from() function.
Code:
import java.time.*; import java.util.*; public class Main { public static void main(String[] args) { ZonedDateTime zoneddatetime = ZonedDateTime.parse("2020-02-25T23:12:31.123+02:00[Europe/Paris]"); ZoneId result = ZoneId.from(zoneddatetime); System.out.println("Zone Id got from "+ "TemporalAccessor object \n" + zoneddatetime + "\nis " + result); } }
Output:
6. public abstract String getId()
This function is used to get a unique time-zone ID for a particular object. The format for an offset-based ID is defined by the getId() method in the Timezone class. The ID given as an output helps to uniquely define the object.
Code:
import java.time.*; import java.util.*; public class Main { public static void main(String[] args) { ZoneId zoneId = ZoneId.of("Asia/Calcutta"); System.out.println("Id using getID(): "+ zoneId.getId()); } }
Output:
7. public String getDisplayName(TextStyle style,Locale locale)
This method helps to provide the textual representation of the zone. The style required as well as the locale of the output required is given as arguments. And thus, the suitable textual representation of the time-zone id is given as an output. In case no textual representation is present, a complete ID is returned as an output.
Code:
import java.time.*; import java.util.*; import java.time.format.TextStyle; public class Main { public static void main(String[] args) { ZoneId zoneId = ZoneId.of("Asia/Calcutta"); String result = zoneId.getDisplayName(TextStyle.SHORT, Locale.ENGLISH); System.out.println("Name of ID using getDisplayName(): "+ result); } }
Output:
8. public abstract ZoneRules getRules()
This function is used to retrieve the rules that are applied for the given ID. These rules give an idea about the calculations to be performed as well as the functionality associated with time-zone like finding an offset for an instant. ZoneRulesProvider supplies these rules. An advanced provider can also make a dynamic update to these rules, but the results may change over time in this case. In case no rule is available or the rules of JRE is different from that of time-zone, a call made to this function may lead to ZoneRulesException.
Code:
import java.time.*; import java.util.*; import java.time.format.TextStyle; public class Main { public static void main(String[] args) { ZoneId zoneId = ZoneId.of("Asia/Calcutta"); System.out.println("Rules are: "+ zoneId.getRules()); } }
Output:
9. public ZoneId normalized()
This method is used to check if the given zoneID contains a fixed Offset and if yes, a ZOneOffset equal to that fixed offset is returned; otherwise, this is returned. The returned ID will also have ZoneRules similar to the given offset, but the result we get from the getId() function is different from what we get here.
Code:
import java.time.*; import java.util.*; import java.time.format.TextStyle; public class Main { public static void main(String[] args) { ZoneId zoneId = ZoneId.of("Asia/Calcutta"); System.out.println("Normalized Id: "+ zoneId.normalized()); } }
Output:
10. public boolean equals(Object obj)
This method helps to compare 2 different times ZoneId objects. This method overrides the equals method in the Object class, which is used to compare value stored in 2 objects. Similarly, here the value of these ZoneID is compared to see if 2 objects are equal or not. And, Accordingly, true and false is returned.
Code:
import java.time.*; import java.util.*; import java.time.format.TextStyle; public class Main { public static void main(String[] args) { ZoneId zoneId = ZoneId.of("Asia/Calcutta"); ZoneId zoneId2 = ZoneId.of("Europe/Paris"); System.out.println("Output of Equals: "+ zoneId.equals(zoneId2)); } }
Output:
11. public int hashCode()
This function is used to get the hash code for a particular ZoneID object. This hashcode is returned in int format.
Code:
import java.time.*; import java.util.*; import java.time.format.TextStyle; public class Main { public static void main(String[] args) { ZoneId zoneId = ZoneId.of("Asia/Calcutta"); ZoneId zoneId2 = ZoneId.of("Europe/Paris"); System.out.println("Output of zoneid hashCode: "+ zoneId.hashCode()); System.out.println("Output of zoneid2 hashCode: "+ zoneId2.hashCode()); } }
Output:
12. public String toString()
This function is used to get the string representation of the time -zone ID that is stored in the particular Offset ID. This function overrides the method toString() of Object class.
Code:
import java.time.*; import java.util.*; import java.time.format.TextStyle; public class Main { public static void main(String[] args) { ZoneId zoneId = ZoneId.of("Asia/Calcutta"); ZoneId zoneId2 = ZoneId.of("Europe/Paris"); System.out.println("Output of zoneid toString: "+ zoneId.toString()); System.out.println("Output of zoneid2 toString: "+ zoneId2.toString()); } }
Output:
Conclusion
ZoneID is a serialized class that is used to store the IDs that identify different ZoneRules that the government frequently updates. Thus at the time of serialization, rules are used instead as they contain the entire data set. Also, calling normalized() on ZoneId returns fixed offset ID in the format of ZoneOffset.
The above is the detailed content of Java ZoneId. For more information, please follow other related articles on the PHP Chinese website!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Linux new version
SublimeText3 Linux latest version