ZoneId class in java represents time-zone such as ‘Asia/Kolkata’. There are two main types of Zone Ids: ZoneOffset Ids that consists of ‘Z’ and start with ‘+’ or ‘-‘. The other type of Ids are offset style Ids such as ‘GMT+2’ or ‘UTC+01:00’. ZoneId class provides a way to convert between Instant and LocalDateTime.
java ZoneId class:
public abstract class ZoneId extends Object implements Serializable
Java ZoneId – Method Summary
Method | Description |
---|---|
boolean equals(Object obj) | Checks if this time-zone id is equal to the other time-zone id. |
static Set<String> getAvailableZoneIds() | Gets the Set of available Zone Ids. |
abstract String getId() | It is used to obtain the unique zone id. |
abstract ZoneRules getRules() | It is used to get the time-zone rules applicable to this zone id. These rules define the calculations that can be performed on this time-zone. |
int hashCode() | This method returns the hash code for this time-zone id. |
static ZoneId systemDefault() | This method returns the Zone id of system default time-zone. |
static ZoneId from(TemporalAccessor temporal) | It obtains an instance of ZoneId from a temporal object. |
ZoneId normalized() | This method normalizes this zone id and returns a ZoneOffset. |
String toString() | This method can be used on a ZoneId to get the zone info as a String. |
static ZoneId ofOffset(String prefix, ZoneOffset offset) | Obtains a Zone Id that has the specified offset. |
String getDisplayName(TextStyle style, Locale locale) | Returns the Zone information in String form such as ‘India Time’ or ‘+02:00’. |
Java ZoneId – of() and now() methods example
Here we are using of()
method to obtain an instance of ZoneId
by providing the Id information. The now()
method is used to obtain the local date and local time from the zone id.
import java.time.*; public class JavaExample { public static void main(String... args) { //Obtaining Zone id ZoneId zid = ZoneId.of("Asia/Kolkata"); //Obtaining local time from the zone id LocalTime time = LocalTime.now(zid); System.out.println(time); //Obtaining local date from the zone id LocalDate date = LocalDate.now(zid); System.out.println(date); } }
Output:
07:57:37.322601 2022-06-13
Java ZoneId – systemDefault() and getDisplayName() methods example
Method systemDefault()
returns the default time zone id and method getDisplayName()
is used to obtain the textual representation of the zone id.
import java.util.Locale; import java.time.ZoneId; import java.time.format.TextStyle; public class JavaExample { public static void main(String[] args) { //getting system default zone id ZoneId zid = ZoneId.systemDefault(); System.out.println(zid); //Displaying textual information of zone System.out.println(zid.getDisplayName(TextStyle.FULL, Locale.ROOT)); } }
Output:
Asia/Kolkata India Time
Java ZoneId – getId() and equals() method example
In this example, you will learn the use of two methods of ZoneId
class that are getId()
and equals()
. The getId()
method returns the zone id information as a String and equals()
compares the two zone ids.
import java.time.ZoneId; public class JavaExample { public static void main(String[] args) { //Getting default time zone id ZoneId zid = ZoneId.systemDefault(); String timezone = zid.getId(); System.out.println("Default time-zone id: "+timezone); //Getting the zone id of India ZoneId zid2 = ZoneId.of("Asia/Kolkata"); //Is zid equal to zid2? System.out.println(zid.equals(zid2)); } }
Output:
Default time-zone id: Asia/Kolkata true
Reference:
Leave a Reply