Welcome to our guide on the Java ZonedDateTime
class methods! The ZonedDateTime
class, part of the java.time
package introduced in Java 8, is essential for representing date and time with a time-zone. This class provides a comprehensive way to handle date-time values in different time zones, making it crucial for applications that operate globally.
Key Points to Know
- Represents Date and Time with Time-Zone: Unlike
LocalDateTime
,ZonedDateTime
includes time-zone information, allowing for accurate date-time handling across different zones. - Immutable and Thread-Safe:
ZonedDateTime
objects are immutable, ensuring consistency and making them thread-safe. - Easy Instance Creation: Create instances for the current date and time with the default system time-zone, specific date and time values with a specified time-zone, or parse them from strings.
- Comprehensive API: Offers a wide range of methods for manipulating and accessing date, time, and time-zone information. You can add/subtract dates and times, modify specific fields, and perform date-time comparisons considering time-zone differences.
- Time-Zone Adjustments: Automatically handles Daylight Saving Time changes and provides methods to convert between time zones.
- Formatting and Parsing:
ZonedDateTime
can be formatted into strings and parsed from strings usingDateTimeFormatter
, supporting both ISO-8601 and custom formats.
In this post, we'll explore the various methods available in the ZonedDateTime
class, providing detailed descriptions and links to separate posts for each method to help you master date-time manipulation with time-zone information in Java.
Java ZonedDateTime Methods
Method | Description |
---|---|
now() | Obtains the current date-time from the system clock in the default time-zone. |
of() | Obtains an instance of ZonedDateTime from a year, month, day, hour, minute, second and nanosecond. |
plusDays() | Returns a copy of this date-time with the specified number of days added. |
minusWeeks() | Returns a copy of this date-time with the specified number of weeks subtracted. |
getZone() | Gets the time-zone, such as ‘Europe/Paris’. |
getDayOfMonth() | Gets the day-of-month field. |
getDayOfWeek() | Gets the day-of-week field. |
getDayOfYear() | Gets the day-of-year field. |
getHour() | Gets the hour-of-day field. |
getMinute() | Gets the minute-of-hour field. |
getMonth() | Gets the month-of-year field. |
getSecond() | Gets the second-of-minute field. |
minusDays() | Returns a copy of this date-time with the specified number of days subtracted. |
minusHours() | Returns a copy of this date-time with the specified number of hours subtracted. |
minusMinutes() | Returns a copy of this date-time with the specified number of minutes subtracted. |
minusMonths() | Returns a copy of this date-time with the specified number of months subtracted. |
minusSeconds() | Returns a copy of this date-time with the specified number of seconds subtracted. |
minusYears() | Returns a copy of this date-time with the specified number of years subtracted. |
parse() | Obtains an instance of ZonedDateTime from a text string such as 2007-12-03T10:15:30+01:00[Europe/Paris] . |
plusDays() | Returns a copy of this date-time with the specified number of days added. |
plusHours() | Returns a copy of this date-time with the specified number of hours added. |
plusMinutes() | Returns a copy of this date-time with the specified number of minutes added. |
plusWeeks() | Returns a copy of this date-time with the specified number of weeks added. |
plusYears() | Returns a copy of this date-time with the specified number of years added. |
toLocalDateTime() | Gets the LocalDateTime part of this date-time. |
toOffsetDateTime() | Gets the OffsetDateTime part of this date-time. |
For more details, visit the Java Documentation.