🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
ZonedDateTime in Java, part of the java.time package, represents a date-time with a time zone. It is useful for handling date and time with specific time zone information.
Table of Contents
- What is
ZonedDateTime? - Creating
ZonedDateTimeInstances - Common Methods
- Examples of
ZonedDateTime - Conclusion
1. What is ZonedDateTime?
ZonedDateTime combines LocalDateTime with ZoneId, providing complete date-time information including the time zone. It is ideal for applications that require precise control over date and time across different time zones.
2. Creating ZonedDateTime Instances
You can create ZonedDateTime instances in several ways:
ZonedDateTime.now(): Obtains the current date-time from the system clock in the default time zone.ZonedDateTime.of(LocalDateTime dateTime, ZoneId zone): Combines aLocalDateTimewith aZoneId.ZonedDateTime.parse(CharSequence text): Parses a string to aZonedDateTimeusing the ISO-8601 format.
3. Common Methods
getZone(): Returns theZoneIdof this date-time.toLocalDateTime(): Converts thisZonedDateTimeto aLocalDateTime.plusDays(long daysToAdd): Returns a copy of this date-time with the specified number of days added.minusHours(long hoursToSubtract): Returns a copy of this date-time with the specified number of hours subtracted.isBefore(ZonedDateTime other): Checks if this date-time is before the specified date-time.isAfter(ZonedDateTime other): Checks if this date-time is after the specified date-time.
4. Examples of ZonedDateTime
Example 1: Getting the Current Date-Time with Zone
This example demonstrates how to get the current date-time with the system's default time zone using ZonedDateTime.now().
import java.time.ZonedDateTime;
public class CurrentZonedDateTimeExample {
public static void main(String[] args) {
ZonedDateTime now = ZonedDateTime.now();
System.out.println("Current Zoned Date-Time: " + now);
}
}
Output:
Current Zoned Date-Time: 2024-06-30T13:13:24.765219+05:30[Asia/Kolkata]
Example 2: Creating a Specific ZonedDateTime
Here, we create a specific ZonedDateTime by combining a LocalDateTime with a ZoneId.
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class SpecificZonedDateTimeExample {
public static void main(String[] args) {
LocalDateTime dateTime = LocalDateTime.of(2023, 6, 30, 14, 30);
ZoneId zone = ZoneId.of("Asia/Kolkata");
ZonedDateTime zonedDateTime = ZonedDateTime.of(dateTime, zone);
System.out.println("Specific Zoned Date-Time: " + zonedDateTime);
}
}
Output:
Specific Zoned Date-Time: 2023-06-30T14:30+05:30[Asia/Kolkata]
Example 3: Parsing a ZonedDateTime String
This example shows how to parse a string into a ZonedDateTime using ZonedDateTime.parse(CharSequence text).
import java.time.ZonedDateTime;
public class ParseZonedDateTimeExample {
public static void main(String[] args) {
ZonedDateTime zonedDateTime = ZonedDateTime.parse("2023-06-30T14:30:00+05:30[Asia/Kolkata]");
System.out.println("Parsed Zoned Date-Time: " + zonedDateTime);
}
}
Output:
Parsed Zoned Date-Time: 2023-06-30T14:30+05:30[Asia/Kolkata]
Example 4: Adding and Subtracting Time
In this example, we demonstrate how to add days and subtract hours from a ZonedDateTime.
import java.time.ZonedDateTime;
public class AddSubtractTimeExample {
public static void main(String[] args) {
ZonedDateTime zonedDateTime = ZonedDateTime.now();
ZonedDateTime nextWeek = zonedDateTime.plusDays(7);
ZonedDateTime lastHour = zonedDateTime.minusHours(1);
System.out.println("Current Zoned Date-Time: " + zonedDateTime);
System.out.println("Next Week: " + nextWeek);
System.out.println("Last Hour: " + lastHour);
}
}
Output:
Current Zoned Date-Time: 2024-06-30T13:13:25.090182+05:30[Asia/Kolkata]
Next Week: 2024-07-07T13:13:25.090182+05:30[Asia/Kolkata]
Last Hour: 2024-06-30T12:13:25.090182+05:30[Asia/Kolkata]
Example 5: Comparing ZonedDateTimes
This example demonstrates how to compare two ZonedDateTime instances using isBefore and isAfter.
import java.time.ZonedDateTime;
public class CompareZonedDateTimesExample {
public static void main(String[] args) {
ZonedDateTime dateTime1 = ZonedDateTime.parse("2023-06-30T10:00:00+05:30[Asia/Kolkata]");
ZonedDateTime dateTime2 = ZonedDateTime.parse("2023-07-01T10:00:00+05:30[Asia/Kolkata]");
System.out.println("Is dateTime1 before dateTime2? " + dateTime1.isBefore(dateTime2));
System.out.println("Is dateTime1 after dateTime2? " + dateTime1.isAfter(dateTime2));
}
}
Output:
Is dateTime1 before dateTime2? true
Is dateTime1 after dateTime2? false
Example 6: Retrieving Zone and LocalDateTime
This example shows how to retrieve the zone and convert the ZonedDateTime to LocalDateTime.
import java.time.ZonedDateTime;
public class ZonedDateTimeComponentsExample {
public static void main(String[] args) {
ZonedDateTime zonedDateTime = ZonedDateTime.now();
System.out.println("Zone: " + zonedDateTime.getZone());
System.out.println("Local Date-Time: " + zonedDateTime.toLocalDateTime());
}
}
Output:
Zone: Asia/Kolkata
Local Date-Time: 2024-06-30T13:13:25.299998
Conclusion
The ZonedDateTime class in Java is used for handling date and time with specific time zone information. It is particularly useful for applications that need to consider time zones in date-time calculations. Using ZonedDateTime can lead to more accurate and clear handling of date-time data in your Java applications.
Comments
Post a Comment
Leave Comment