
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get the ID of this timezone in Java
To get the ID of the current timezone in Java, we use the ZoneId class that belongs to java.time package. This provides a way to work with different timezones and their IDs.
In Java, the ZoneId class represents a time zone identifier, such as India/Europe. The ZoneId can be used to retrieve the unique ID of the current timezone.
To Get the ID of this timezone in Java is quite easy. Let's learn the following methods:
Using ZoneId Class
To get the ID, we have a class named ZoneId that belongs to java.time package that represents a time zone identifier, such as Europe/Paris. It is used to work and manipulate time zone data.
Example
import java.time.ZoneId; import java.time.ZonedDateTime; public class GetCurrentTimeZoneID { public static void main(String[] args) { ZonedDateTime now = ZonedDateTime.now(); ZoneId zoneId = now.getZone(); System.out.println("The current timezone ID is: " + zoneId); } }
Output
The above program produce the following result ?
The current timezone ID is: Asia/Kolkata
Using ZoneId.systemDefault() Method
To get the ID, we also have a method named ZoneId.systemDefault() which is a static method of the ZoneId class in the java.time package. It returns the system's default time zone.
Example
import java.time.ZoneId; public class GetCurrentTimeZoneID { public static void main(String[] args) { ZoneId zoneId = ZoneId.systemDefault(); System.out.println("The current timezone ID is: " + zoneId); } }
Output
The above program produce the following result ?
The current timezone ID is: Asia/Kolkata
Using TimeZone.getDefault().toZoneId() Method
To get the ID, we have another method named TimeZone.getDefault().toZoneId() method in Java is used to obtain the system's default time zone and convert it to a ZoneId object.
Example
import java.util.TimeZone; import java.time.ZoneId; public class GetCurrentTimeZoneID { public static void main(String[] args) { ZoneId zoneId = TimeZone.getDefault().toZoneId(); System.out.println("The current timezone ID is: " + zoneId); } }
Output
The above program produce the following result ?
The current timezone ID is: Asia/Kolkata