The ZonedDateTime
class in Java 8 is part of the java.time
package and represents a date-time with a time-zone in the ISO-8601 calendar system, such as 2007-12-03T10:15:30+01:00[Europe/Paris]
. It is an immutable class that is widely used for date and time operations in Java 8 and later versions.
Methods of the ZonedDateTime Class with Examples and Output
1. now()
Obtains the current date-time from the system clock in the default time-zone.
Example:
import java.time.ZonedDateTime;
public class ZonedDateTimeExample {
public static void main(String[] args) {
ZonedDateTime current = ZonedDateTime.now();
System.out.println("Current date-time: " + current);
}
}
Output:
Current date-time: 2024-07-05T13:55:58.110579+05:30[Asia/Kolkata]
2. now(Clock clock)
Obtains the current date-time from the specified clock.
Example:
import java.time.ZonedDateTime;
import java.time.Clock;
public class ZonedDateTimeExample {
public static void main(String[] args) {
Clock clock = Clock.systemUTC();
ZonedDateTime current = ZonedDateTime.now(clock);
System.out.println("Current date-time: " + current);
}
}
Output:
Current date-time: 2024-07-05T08:25:58.239156Z
3. now(ZoneId zone)
Obtains the current date-time from the system clock in the specified time-zone.
Example:
import java.time.ZonedDateTime;
import java.time.ZoneId;
public class ZonedDateTimeExample {
public static void main(String[] args) {
ZoneId zoneId = ZoneId.of("Asia/Kolkata");
ZonedDateTime current = ZonedDateTime.now(zoneId);
System.out.println("Current date-time in Asia/Kolkata: " + current);
}
}
Output:
Current date-time in Asia/Kolkata: 2024-07-05T13:55:58.336314+05:30[Asia/Kolkata]
4. of(LocalDate date, LocalTime time, ZoneId zone)
Obtains an instance of ZonedDateTime
from a local date, local time, and zone.
Example:
import java.time.ZonedDateTime;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
public class ZonedDateTimeExample {
public static void main(String[] args) {
LocalDate date = LocalDate.of(2024, 7, 5);
LocalTime time = LocalTime.of(10, 15, 30);
ZoneId zoneId = ZoneId.of("Europe/Paris");
ZonedDateTime zonedDateTime = ZonedDateTime.of(date, time, zoneId);
System.out.println("ZonedDateTime: " + zonedDateTime);
}
}
Output:
ZonedDateTime: 2024-07-05T10:15:30+02:00[Europe/Paris]
5. of(LocalDateTime dateTime, ZoneId zone)
Obtains an instance of ZonedDateTime
from a local date-time and zone.
Example:
import java.time.ZonedDateTime;
import java.time.LocalDateTime;
import java.time.ZoneId;
public class ZonedDateTimeExample {
public static void main(String[] args) {
LocalDateTime dateTime = LocalDateTime.of(2024, 7, 5, 10, 15, 30);
ZoneId zoneId = ZoneId.of("Europe/Paris");
ZonedDateTime zonedDateTime = ZonedDateTime.of(dateTime, zoneId);
System.out.println("ZonedDateTime: " + zonedDateTime);
}
}
Output:
ZonedDateTime: 2024-07-05T10:15:30+02:00[Europe/Paris]
6. ofInstant(Instant instant, ZoneId zone)
Obtains an instance of ZonedDateTime
from an Instant
and zone.
Example:
import java.time.ZonedDateTime;
import java.time.Instant;
import java.time.ZoneId;
public class ZonedDateTimeExample {
public static void main(String[] args) {
Instant instant = Instant.now();
ZoneId zoneId = ZoneId.of("Europe/Paris");
ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(instant, zoneId);
System.out.println("ZonedDateTime: " + zonedDateTime);
}
}
Output:
ZonedDateTime: 2024-07-05T10:25:58.618486+02:00[Europe/Paris]
7. ofStrict(LocalDateTime dateTime, ZoneOffset offset, ZoneId zone)
Obtains an instance of ZonedDateTime
from a local date-time, time-zone, and an explicit zone offset.
Example:
import java.time.ZonedDateTime;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
public class ZonedDateTimeExample {
public static void main(String[] args) {
LocalDateTime dateTime = LocalDateTime.of(2024, 7, 5, 10, 15, 30);
ZoneOffset offset = ZoneOffset.of("+02:00");
ZoneId zoneId = ZoneId.of("Europe/Paris");
ZonedDateTime zonedDateTime = ZonedDateTime.ofStrict(dateTime, offset, zoneId);
System.out.println("ZonedDateTime: " + zonedDateTime);
}
}
Output:
ZonedDateTime: 2024-07-05T10:15:30+02:00[Europe/Paris]
8. from(TemporalAccessor temporal)
Obtains an instance of ZonedDateTime
from a temporal object.
Example:
import java.time.ZonedDateTime;
import java.time.LocalDateTime;
public class ZonedDateTimeExample {
public static void main(String[] args) {
LocalDateTime dateTime = LocalDateTime.of(2024, 7, 5, 10, 15, 30);
ZonedDateTime zonedDateTime = ZonedDateTime.from(dateTime.atZone(ZonedDateTime.now().getZone()));
System.out.println("ZonedDateTime: " + zonedDateTime);
}
}
Output:
ZonedDateTime: 2024-07-05T10:15:30+05:30[Asia/Kolkata]
9. parse(CharSequence text)
Obtains an instance of ZonedDateTime
from a text string such as 2007-12-03T10:15:30+01:00[Europe/Paris]
.
Example:
import java.time.ZonedDateTime;
public class ZonedDateTimeExample {
public static void main(String[] args) {
String text = "2024-07-05T10:15:30+02:00[Europe/Paris]";
ZonedDateTime zonedDateTime = ZonedDateTime.parse(text);
System.out.println("Parsed ZonedDateTime: " + zonedDateTime);
}
}
Output:
Parsed ZonedDateTime: 2024-07-05T10:15:30+02:00[Europe/Paris]
10. plusYears(long yearsToAdd)
Returns a copy of this ZonedDateTime
with the specified number of years added.
Example:
import java.time.ZonedDateTime;
public class ZonedDateTimeExample {
public static void main(String[] args) {
ZonedDateTime zonedDateTime = ZonedDateTime.now();
ZonedDateTime newZonedDateTime = zonedDateTime.plusYears(2);
System.out.println("ZonedDateTime plus 2 years: " + newZonedDateTime);
}
}
Output:
ZonedDateTime plus 2 years: 2026-07-05T13:55:59.016011+05:30[Asia/Kolkata]
11. plusMonths(long monthsToAdd)
Returns a copy of this ZonedDateTime
with the specified number of months added.
Example:
import java.time.ZonedDateTime;
public class ZonedDateTimeExample {
public static void main(String[] args) {
ZonedDateTime zonedDateTime = ZonedDateTime.now();
ZonedDateTime newZonedDateTime = zonedDateTime.plusMonths(3);
System.out.println("ZonedDateTime plus 3 months: " + newZonedDateTime);
}
}
Output:
ZonedDateTime plus 3 months: 2024-10-05T13:55:59.113396+05:30[Asia/Kolkata]
12. plusWeeks(long weeksToAdd)
Returns a copy of this ZonedDateTime
with the specified number of weeks added.
**Example:
**
import java.time.ZonedDateTime;
public class ZonedDateTimeExample {
public static void main(String[] args) {
ZonedDateTime zonedDateTime = ZonedDateTime.now();
ZonedDateTime newZonedDateTime = zonedDateTime.plusWeeks(2);
System.out.println("ZonedDateTime plus 2 weeks: " + newZonedDateTime);
}
}
Output:
ZonedDateTime plus 2 weeks: 2024-07-19T13:55:59.209396+05:30[Asia/Kolkata]
13. plusDays(long daysToAdd)
Returns a copy of this ZonedDateTime
with the specified number of days added.
Example:
import java.time.ZonedDateTime;
public class ZonedDateTimeExample {
public static void main(String[] args) {
ZonedDateTime zonedDateTime = ZonedDateTime.now();
ZonedDateTime newZonedDateTime = zonedDateTime.plusDays(10);
System.out.println("ZonedDateTime plus 10 days: " + newZonedDateTime);
}
}
Output:
ZonedDateTime plus 10 days: 2024-07-15T13:55:59.305915+05:30[Asia/Kolkata]
14. plusHours(long hoursToAdd)
Returns a copy of this ZonedDateTime
with the specified number of hours added.
Example:
import java.time.ZonedDateTime;
public class ZonedDateTimeExample {
public static void main(String[] args) {
ZonedDateTime zonedDateTime = ZonedDateTime.now();
ZonedDateTime newZonedDateTime = zonedDateTime.plusHours(5);
System.out.println("ZonedDateTime plus 5 hours: " + newZonedDateTime);
}
}
Output:
ZonedDateTime plus 5 hours: 2024-07-05T18:55:59.400116+05:30[Asia/Kolkata]
15. plusMinutes(long minutesToAdd)
Returns a copy of this ZonedDateTime
with the specified number of minutes added.
Example:
import java.time.ZonedDateTime;
public class ZonedDateTimeExample {
public static void main(String[] args) {
ZonedDateTime zonedDateTime = ZonedDateTime.now();
ZonedDateTime newZonedDateTime = zonedDateTime.plusMinutes(45);
System.out.println("ZonedDateTime plus 45 minutes: " + newZonedDateTime);
}
}
Output:
ZonedDateTime plus 45 minutes: 2024-07-05T14:40:59.497663+05:30[Asia/Kolkata]
16. plusSeconds(long secondsToAdd)
Returns a copy of this ZonedDateTime
with the specified number of seconds added.
Example:
import java.time.ZonedDateTime;
public class ZonedDateTimeExample {
public static void main(String[] args) {
ZonedDateTime zonedDateTime = ZonedDateTime.now();
ZonedDateTime newZonedDateTime = zonedDateTime.plusSeconds(120);
System.out.println("ZonedDateTime plus 120 seconds: " + newZonedDateTime);
}
}
Output:
ZonedDateTime plus 120 seconds: 2024-07-05T13:57:59.598519+05:30[Asia/Kolkata]
17. plusNanos(long nanosToAdd)
Returns a copy of this ZonedDateTime
with the specified number of nanoseconds added.
Example:
import java.time.ZonedDateTime;
public class ZonedDateTimeExample {
public static void main(String[] args) {
ZonedDateTime zonedDateTime = ZonedDateTime.now();
ZonedDateTime newZonedDateTime = zonedDateTime.plusNanos(1000000);
System.out.println("ZonedDateTime plus 1 million nanoseconds: " + newZonedDateTime);
}
}
Output:
ZonedDateTime plus 1 million nanoseconds: 2024-07-05T13:55:59.697911+05:30[Asia/Kolkata]
18. minusYears(long yearsToSubtract)
Returns a copy of this ZonedDateTime
with the specified number of years subtracted.
Example:
import java.time.ZonedDateTime;
public class ZonedDateTimeExample {
public static void main(String[] args) {
ZonedDateTime zonedDateTime = ZonedDateTime.now();
ZonedDateTime newZonedDateTime = zonedDateTime.minusYears(3);
System.out.println("ZonedDateTime minus 3 years: " + newZonedDateTime);
}
}
Output:
ZonedDateTime minus 3 years: 2021-07-05T13:55:59.792414+05:30[Asia/Kolkata]
19. minusMonths(long monthsToSubtract)
Returns a copy of this ZonedDateTime
with the specified number of months subtracted.
Example:
import java.time.ZonedDateTime;
public class ZonedDateTimeExample {
public static void main(String[] args) {
ZonedDateTime zonedDateTime = ZonedDateTime.now();
ZonedDateTime newZonedDateTime = zonedDateTime.minusMonths(4);
System.out.println("ZonedDateTime minus 4 months: " + newZonedDateTime);
}
}
Output:
ZonedDateTime minus 4 months: 2024-03-05T13:55:59.881642+05:30[Asia/Kolkata]
20. minusWeeks(long weeksToSubtract)
Returns a copy of this ZonedDateTime
with the specified number of weeks subtracted.
Example:
import java.time.ZonedDateTime;
public class ZonedDateTimeExample {
public static void main(String[] args) {
ZonedDateTime zonedDateTime = ZonedDateTime.now();
ZonedDateTime newZonedDateTime = zonedDateTime.minusWeeks(6);
System.out.println("ZonedDateTime minus 6 weeks: " + newZonedDateTime);
}
}
Output:
ZonedDateTime minus 6 weeks: 2024-05-24T13:55:59.974968+05:30[Asia/Kolkata]
21. minusDays(long daysToSubtract)
Returns a copy of this ZonedDateTime
with the specified number of days subtracted.
Example:
import java.time.ZonedDateTime;
public class ZonedDateTimeExample {
public static void main(String[] args) {
ZonedDateTime zonedDateTime = ZonedDateTime.now();
ZonedDateTime newZonedDateTime = zonedDateTime.minusDays(15);
System.out.println("ZonedDateTime minus 15 days: " + newZonedDateTime);
}
}
Output:
ZonedDateTime minus 15 days: 2024-06-20T13:56:00.072299+05:30[Asia/Kolkata]
22. minusHours(long hoursToSubtract)
Returns a copy of this ZonedDateTime
with the specified number of hours subtracted.
Example:
import java.time.ZonedDateTime;
public class ZonedDateTimeExample {
public static void main(String[] args) {
ZonedDateTime zonedDateTime = ZonedDateTime.now();
ZonedDateTime newZonedDateTime = zonedDateTime.minusHours(5);
System.out.println("ZonedDateTime minus 5 hours: " + newZonedDateTime);
}
}
Output:
ZonedDateTime minus 5 hours: 2024-07-05T08:56:00.161536+05:30[Asia/Kolkata]
23. minusMinutes(long minutesToSubtract)
Returns a copy of this ZonedDateTime
with the specified number of minutes subtracted.
Example:
import java.time.ZonedDateTime;
public class ZonedDateTimeExample {
public static void main(String[] args) {
ZonedDateTime zonedDateTime = ZonedDateTime.now();
ZonedDateTime newZonedDateTime = zonedDateTime.minusMinutes(45);
System.out.println("ZonedDateTime minus 45 minutes: " + newZonedDateTime);
}
}
Output:
ZonedDateTime minus 45 minutes: 2024-07-05T13:11:00.261050+05:30[Asia/Kolkata]
24. minusSeconds(long secondsToSubtract)
Returns a copy of this ZonedDateTime
with the specified number of seconds subtracted.
Example:
import java.time.ZonedDateTime;
public class ZonedDateTimeExample {
public static void main(String[] args) {
ZonedDateTime zonedDateTime = ZonedDateTime.now();
ZonedDateTime newZonedDateTime = zonedDateTime.minusSeconds(120);
System.out.println("ZonedDateTime minus 120 seconds: " + newZonedDateTime);
}
}
Output:
ZonedDateTime minus 120 seconds: 2024-07-05T13:54:00.364502+05:30[Asia/Kolkata]
25. minusNanos(long nanosToSubtract)
Returns a copy of this ZonedDateTime
with the specified number of nanoseconds subtracted.
Example:
import java.time.ZonedDateTime;
public class ZonedDateTimeExample {
public static void main(String[] args) {
ZonedDateTime zonedDateTime = ZonedDateTime.now();
ZonedDateTime newZonedDateTime = zonedDateTime.minusNanos(1000000);
System.out.println("ZonedDateTime minus 1 million nanoseconds: " + newZonedDateTime);
}
}
Output:
ZonedDateTime minus 1 million nanoseconds: 2024-07-05T13:56:00.452356+05:30[Asia/Kolkata]
26. toLocalDate()
Gets the LocalDate
part of this date-time.
Example:
import java.time.ZonedDateTime;
import java.time.LocalDate;
public class ZonedDateTimeExample {
public static void main(String[] args) {
ZonedDateTime zonedDateTime = ZonedDateTime.now();
LocalDate localDate = zonedDateTime.toLocalDate();
System.out.println("LocalDate: " + localDate);
}
}
Output:
LocalDate: 2024-07-05
27. toLocalTime()
Gets the LocalTime
part of this date-time.
Example:
import java.time.ZonedDateTime;
import java.time.LocalTime;
public class ZonedDateTimeExample {
public static void main(String[] args) {
ZonedDateTime zonedDateTime = ZonedDateTime.now();
LocalTime localTime = zonedDateTime.toLocalTime();
System.out.println("LocalTime: " + localTime);
}
}
Output:
LocalTime: 13:56:00.636962
28. toLocalDateTime()
Gets the LocalDateTime
part of this date-time.
Example:
import java.time.ZonedDateTime;
import java.time.LocalDateTime;
public class ZonedDateTimeExample {
public static void main(String[] args) {
ZonedDateTime zonedDateTime = ZonedDateTime.now();
LocalDateTime localDateTime = zonedDateTime.toLocalDateTime();
System.out.println("LocalDateTime: " + localDateTime);
}
}
Output:
LocalDateTime: 2024-07-05T13:56:00.757723
29. toOffsetDateTime()
Gets the OffsetDateTime
part of this date-time.
Example:
import java.time.ZonedDateTime;
import java.time.OffsetDateTime;
public class ZonedDateTimeExample {
public static void main(String[] args) {
ZonedDateTime zonedDateTime = ZonedDateTime.now();
OffsetDateTime offsetDateTime = zonedDateTime.toOffsetDateTime();
System.out.println("OffsetDateTime: " + offsetDateTime);
}
}
Output:
OffsetDateTime: 2024-07-05T13:56:00.886077+05:30
30. withZoneSameInstant(ZoneId zone)
Returns a copy of this date-time with a different time-zone, retaining the instant.
Example:
import java.time.ZonedDateTime;
import java.time.ZoneId;
public class ZonedDateTimeExample {
public static void main(String[] args) {
ZonedDateTime zonedDateTime = ZonedDateTime.now();
ZoneId newZone = ZoneId.of("America/New_York");
ZonedDateTime newZonedDateTime = zonedDateTime.withZoneSameInstant(newZone);
System.out.println("ZonedDateTime with new zone: " + newZonedDateTime);
}
}
Output:
ZonedDateTime with new zone: 2024-07-05T04:26:00.980552-04:00[America/New_York]
31. withZoneSameLocal(ZoneId zone)
Returns a copy of this date-time with a different time-zone, retaining the local date-time.
Example:
import java.time.ZonedDateTime;
import java.time.ZoneId;
public class ZonedDateTimeExample {
public static void main(String[] args) {
ZonedDateTime zonedDateTime = ZonedDateTime.now();
ZoneId newZone = ZoneId.of("America/New_York");
ZonedDateTime newZonedDateTime = zonedDateTime.withZoneSameLocal(newZone);
System.out.println("ZonedDateTime with same local date-time in new zone: " + newZonedDateTime);
}
}
Output:
ZonedDateTime with same local date-time in new zone: 2024-07-05T13:56:01.070193-04:00[America/New_York]
32. withEarlierOffsetAtOverlap()
Returns a copy of this date-time with the earlier offset during a time-zone overlap.
Example:
import java.time.ZonedDateTime;
import java.time.ZoneId;
public class ZonedDateTimeExample {
public static void main(String[] args) {
ZonedDateTime zonedDateTime = ZonedDateTime.now(ZoneId.of("Europe/Paris"));
ZonedDateTime newZonedDateTime = zonedDateTime.withEarlierOffsetAtOverlap();
System.out.println("ZonedDateTime with earlier offset at overlap: " + newZonedDateTime);
}
}
Output:
ZonedDateTime with earlier offset at overlap: 2024-07-05T10:26:01.163380+02:00[Europe/Paris]
33. withLaterOffsetAtOverlap()
Returns a copy of this date-time with the later offset during a time-zone overlap.
Example:
import java.time.ZonedDateTime;
import java.time.ZoneId;
public class ZonedDateTimeExample {
public static void main(String[] args) {
ZonedDateTime zonedDateTime = ZonedDateTime.now(ZoneId.of("Europe/Paris"));
ZonedDateTime newZonedDateTime = zonedDateTime.withLaterOffsetAtOverlap();
System.out.println("ZonedDateTime with later offset at overlap: " + newZonedDateTime);
}
}
Output:
ZonedDateTime with later offset at overlap: 2024-07-05T10:26:01.236695+02:00[Europe/Paris]
34. withYear(int year)
Returns a copy of this date-time with the year altered.
Example:
import java.time.ZonedDateTime;
public class ZonedDateTimeExample {
public static void main(String[] args) {
ZonedDateTime zonedDateTime = ZonedDateTime.now();
ZonedDateTime newZonedDateTime = zonedDateTime.withYear(2025);
System.out.println("ZonedDateTime with altered year: " + newZonedDateTime);
}
}
Output:
ZonedDateTime with altered year: 2025-07-05T13:56:01.310694+05:30[Asia/Kolkata]
35. withMonth(int month)
Returns a copy of this date-time with the month-of-year altered.
Example:
import java.time.ZonedDateTime;
public class ZonedDateTimeExample {
public static void main(String[] args) {
ZonedDateTime zonedDateTime = ZonedDateTime.now();
ZonedDateTime newZonedDateTime = zonedDateTime.withMonth(12);
System.out.println("ZonedDateTime with altered month: " + newZonedDateTime);
}
}
Output:
ZonedDateTime with altered month: 2024-12-05T13:56:01.402228+05:30[Asia/Kolkata]
36. withDayOfMonth(int dayOfMonth)
Returns a copy of this date-time with the day-of-month altered.
Example:
import java.time.ZonedDateTime;
public class ZonedDateTimeExample {
public static void main(String[] args) {
ZonedDateTime zonedDateTime = ZonedDateTime.now();
ZonedDateTime newZonedDateTime = zonedDateTime.withDayOfMonth(15);
System.out.println("ZonedDateTime with altered day-of-month: " + newZonedDateTime);
}
}
Output:
ZonedDateTime with altered day-of-month: 2024-07-15T13:56:01.492144+05:30[Asia/Kolkata]
37. withDayOfYear(int dayOfYear)
Returns a copy of this date-time with the day-of-year altered.
Example:
import java.time.ZonedDateTime;
public class ZonedDateTimeExample {
public static void main(String[] args) {
ZonedDateTime zonedDateTime = ZonedDateTime.now();
ZonedDateTime newZonedDateTime = zonedDateTime.withDayOfYear(200);
System
.out.println("ZonedDateTime with altered day-of-year: " + newZonedDateTime);
}
}
Output:
ZonedDateTime with altered day-of-year: 2024-07-18T13:56:01.580819+05:30[Asia/Kolkata]
38. withHour(int hour)
Returns a copy of this date-time with the hour-of-day altered.
Example:
import java.time.ZonedDateTime;
public class ZonedDateTimeExample {
public static void main(String[] args) {
ZonedDateTime zonedDateTime = ZonedDateTime.now();
ZonedDateTime newZonedDateTime = zonedDateTime.withHour(15);
System.out.println("ZonedDateTime with altered hour: " + newZonedDateTime);
}
}
Output:
ZonedDateTime with altered hour: 2024-07-05T15:56:01.670686+05:30[Asia/Kolkata]
39. withMinute(int minute)
Returns a copy of this date-time with the minute-of-hour altered.
Example:
import java.time.ZonedDateTime;
public class ZonedDateTimeExample {
public static void main(String[] args) {
ZonedDateTime zonedDateTime = ZonedDateTime.now();
ZonedDateTime newZonedDateTime = zonedDateTime.withMinute(45);
System.out.println("ZonedDateTime with altered minute: " + newZonedDateTime);
}
}
Output:
ZonedDateTime with altered minute: 2024-07-05T13:45:01.761901+05:30[Asia/Kolkata]
40. withSecond(int second)
Returns a copy of this date-time with the second-of-minute altered.
Example:
import java.time.ZonedDateTime;
public class ZonedDateTimeExample {
public static void main(String[] args) {
ZonedDateTime zonedDateTime = ZonedDateTime.now();
ZonedDateTime newZonedDateTime = zonedDateTime.withSecond(45);
System.out.println("ZonedDateTime with altered second: " + newZonedDateTime);
}
}
Output:
ZonedDateTime with altered second: 2024-07-05T13:56:45.847707+05:30[Asia/Kolkata]
41. withNano(int nanoOfSecond)
Returns a copy of this date-time with the nano-of-second altered.
Example:
import java.time.ZonedDateTime;
public class ZonedDateTimeExample {
public static void main(String[] args) {
ZonedDateTime zonedDateTime = ZonedDateTime.now();
ZonedDateTime newZonedDateTime = zonedDateTime.withNano(500000000);
System.out.println("ZonedDateTime with altered nano-of-second: " + newZonedDateTime);
}
}
Output:
ZonedDateTime with altered nano-of-second: 2024-07-05T13:56:01.500+05:30[Asia/Kolkata]
42. toInstant()
Converts this date-time to an Instant
.
Example:
import java.time.ZonedDateTime;
import java.time.Instant;
public class ZonedDateTimeExample {
public static void main(String[] args) {
ZonedDateTime zonedDateTime = ZonedDateTime.now();
Instant instant = zonedDateTime.toInstant();
System.out.println("Instant: " + instant);
}
}
Output:
Instant: 2024-07-05T08:26:02.028982Z
43. getOffset()
Gets the zone offset, such as +01:00
.
Example:
import java.time.ZonedDateTime;
import java.time.ZoneOffset;
public class ZonedDateTimeExample {
public static void main(String[] args) {
ZonedDateTime zonedDateTime = ZonedDateTime.now();
ZoneOffset offset = zonedDateTime.getOffset();
System.out.println("Zone Offset: " + offset);
}
}
Output:
Zone Offset: +05:30
44. getZone()
Gets the time-zone, such as Europe/Paris
.
Example:
import java.time.ZonedDateTime;
import java.time.ZoneId;
public class ZonedDateTimeExample {
public static void main(String[] args) {
ZonedDateTime zonedDateTime = ZonedDateTime.now();
ZoneId zone = zonedDateTime.getZone();
System.out.println("Zone: " + zone);
}
}
Output:
Zone: Asia/Kolkata
45. toEpochSecond()
Converts this ZonedDateTime
to the number of seconds since the epoch of 1970-01-01T00:00:00Z.
Example:
import java.time.ZonedDateTime;
public class ZonedDateTimeExample {
public static void main(String[] args) {
ZonedDateTime zonedDateTime = ZonedDateTime.now();
long epochSecond = zonedDateTime.toEpochSecond();
System.out.println("Epoch Second: " + epochSecond);
}
}
Output:
Epoch Second: 1720167962
46. isBefore(ZonedDateTime other)
Checks if this date-time is before the specified date-time.
Example:
import java.time.ZonedDateTime;
public class ZonedDateTimeExample {
public static void main(String[] args) {
ZonedDateTime zonedDateTime1 = ZonedDateTime.now();
ZonedDateTime zonedDateTime2 = zonedDateTime1.plusHours(1);
boolean isBefore = zonedDateTime1.isBefore(zonedDateTime2);
System.out.println("Is before: " + isBefore);
}
}
Output:
Is before: true
47. isAfter(ZonedDateTime other)
Checks if this date-time is after the specified date-time.
Example:
import java.time.ZonedDateTime;
public class ZonedDateTimeExample {
public static void main(String[] args) {
ZonedDateTime zonedDateTime1 = ZonedDateTime.now();
ZonedDateTime zonedDateTime2 = zonedDateTime1.minusHours(1);
boolean isAfter = zonedDateTime1.isAfter(zonedDateTime2);
System.out.println("Is after: " + isAfter);
}
}
Output:
Is after: true
48. isEqual(ZonedDateTime other)
Checks if this date-time is equal to the specified date-time.
Example:
import java.time.ZonedDateTime;
public class ZonedDateTimeExample {
public static void main(String[] args) {
ZonedDateTime zonedDateTime1 = ZonedDateTime.now();
ZonedDateTime zonedDateTime2 = ZonedDateTime.from(zonedDateTime1);
boolean isEqual = zonedDateTime1.isEqual(zonedDateTime2);
System.out.println("Is equal: " + isEqual);
}
}
Output:
Is equal: true
49. equals(Object obj)
Compares this ZonedDateTime
to another date-time.
Example:
import java.time.ZonedDateTime;
public class ZonedDateTimeExample {
public static void main(String[] args) {
ZonedDateTime zonedDateTime1 = ZonedDateTime.now();
ZonedDateTime zonedDateTime2 = ZonedDateTime.from(zonedDateTime1);
boolean equals = zonedDateTime1.equals(zonedDateTime2);
System.out.println("Equals: " + equals);
}
}
Output:
Equals: true
50. toString()
Outputs this date-time as a String
, such as 2007-12-03T10:15:30+01:00[Europe/Paris]
.
Example:
import java.time.ZonedDateTime;
public class ZonedDateTimeExample {
public static void main(String[] args) {
ZonedDateTime zonedDateTime = ZonedDateTime.now();
System.out.println("ZonedDateTime: " + zonedDateTime.toString());
}
}
Output:
ZonedDateTime: 2024-07-05T13:56:02.745366+05:30[Asia/Kolkata]
This guide covers the essential methods provided by the ZonedDateTime
class in Java 8, demonstrating how to create, manipulate, and query date-time objects with time zones. Each method is illustrated with working examples to help you understand how to use them effectively in your applications.
Comments
Post a Comment
Leave Comment