Java ZoneId class

Java ZoneId class

Java ZoneId class specifies a time zone identifier and provides a rule for converting between an Instant and a LocalDateTime. It inherits Object class and implements the Serializable interface.


Java ZoneId class declaration

Let’s see the declaration of java.time.ZoneId class.

  1. public abstract class ZoneId extends Object implements Serializable

Methods of Java ZoneId

Method Description
String getDisplayName(TextStyle style, Locale locale) It is used to get the textual representation of the zone, such as ‘India Time’ or ‘+05:30’.
abstract String getId() It is used to get the unique time-zone ID.
static ZoneId of(String zoneId) It is used to obtain an instance of ZoneId from an ID ensuring that the ID is valid and available for use.
static ZoneId systemDefault() It is used to get the system default time-zone.
boolean equals(Object obj) It is used to check if this time-zone ID is equal to another time-zone ID.

 

Java ZoneId class Example

  1. import java.time.*;
  2. public class ZoneIdExample {
  3.   public static void main(String… args) {
  4.     ZoneId zoneid1 = ZoneId.of(“Asia/Kolkata”);
  5.     ZoneId zoneid2 = ZoneId.of(“Asia/Tokyo”);
  6.     LocalTime id1 = LocalTime.now(zoneid1);
  7.     LocalTime id2 = LocalTime.now(zoneid2);
  8.     System.out.println(id1);
  9.     System.out.println(id2);
  10.     System.out.println(id1.isBefore(id2));
  11.   }
  12. }

Output:

14:28:58.230
17:58:58.230
true

Java ZoneId class Example: systemDefault()

  1. import java.time.ZoneId;
  2. public class ZoneIdExample {
  3.   public static void main(String[] args) {
  4.     ZoneId zone = ZoneId.systemDefault();
  5.     System.out.println(zone);
  6.   }
  7. }

Output:

Asia/Kolkata

Java ZoneId class Example: getId()

  1. import java.time.ZoneId;
  2. public class ZoneIdExample {
  3.   public static void main(String[] args) {
  4.     ZoneId z = ZoneId.systemDefault();
  5.     String s = z.getId();
  6.     System.out.println(s);
  7.   }
  8. }

Output:

Asia/Kolkata

Java ZoneId class Example: getDisplayName()

  1. import java.util.Locale;
  2. import java.time.ZoneId;
  3. import java.time.format.TextStyle;
  4. public class ZoneIdExample {
  5.   public static void main(String[] args) {
  6.     ZoneId z = ZoneId.systemDefault();
  7.     System.out.println(z.getDisplayName(TextStyle.FULL, Locale.ROOT));
  8.   }
  9. }

Output:

India Time
Next TopicJava ZoneOffset

 

java for beginners
java for beginners
java for beginners
CodSir //course // books
...

WikiSir

Java ZoneId class Java ZoneId class specifies a time zone identifier and provides a rule for converting between an Instant and a LocalDateTime. It inherits Object class and implements the Serializable interface. Java ZoneId class declaration Let’s see the declaration of java.time.ZoneId class. public abstract class ZoneId extends Object implements Serializable Methods of Java ZoneId Method Description String getDisplayName(TextStyle style, Locale locale) […]

Tags:

....
...