JDK Tutorials - Herong's Tutorial Examples
∟Java Date-Time API
∟java.time.ZonedDateTime - Calendar and Timezone
This section describes the java.time.ZonedDateTime class that represents a moment of time expressed in 3 components, ISO 8601 date, time of day, and timezone.
java.time.ZonedDateTime is the second class of the Date-Time API introduced in JDK 1.8
that can be used to present a moment of time.
A java.time.ZonedDateTime object represents a time with 3 components:
- Timezone - Representing a predefined geographical region with certain local time rules like
what is the time offset against UTC, and when to use daylight saving time.
For example, "Europe/Paris" a timezone with +01:00 UTC offset as the standard local time
and +02:00 as the daylight saving time in the summer.
This component can be expressed as the UTC offset followed by the timezone ID like "+02:00[Europe/Paris]".
- Local-Date - Representing a date in ISO 8601 calendar adjusted to the local time in the given timezone.
This component can be expressed as a year, month, and day combination like "2014-04-01".
- Local-Time - Representing local time of the day in the given timezone.
This component can be expressed as a hour, minute, second and fraction of second combination like
"07:48:41.750123456".
The java.time.ZonedDateTime class is designed mainly for manipulating times mapped a specific timezone to
provide time references to local activities.
Its operations respect ISO 8601 calendar and local timezone rules.
Main methods offered on the java.time.Instant class are:
- of() - A static method returning a ZonedDateTime object representing the moment of time
constructed by a given ISO 8601 date, time of day, and local timezone information.
- ofInstant() - A static method returning a ZonedDateTime object representing the moment of time
converted from a given Instant object and a given local timezone.
- getYear() - An object method returning the year value of this ZonedDateTime object.
- getMonthValue() - An object method returning the month-of-year value of this ZonedDateTime object.
- getDayOfMonth() - An object method returning the day-of-month value of this ZonedDateTime object.
- getDayOfWeek() - An object method returning the day-of-week value of this ZonedDateTime object.
- getHour() - An object method returning the hour-of-day value of this ZonedDateTime object.
- getMinute() - An object method returning the minute-of-hour value of this ZonedDateTime object.
- getSecond() - An object method returning the second-of-minute value of this ZonedDateTime object.
- getNano() - An object method returning the Nanosecond-of-Second value of this ZonedDateTime object.
- getOffset() - An object method returning the UTC offset information of this ZonedDateTime object.
- getZone() - An object method returning the timezone information of this ZonedDateTime object.
- get() - An object method returning the value of a given temporal field,
NANO_OF_SECOND, SECOND_OF_MINUTE, MINUTE_OF_HOUR, HOUR_OF_DAY, DAY_OF_WEEK, DAY_OF_MONTH, MONTH_OF_YEAR, YEAR, INSTANT_SECONDS, OFFSET_SECONDS, etc.
from this ZonedDateTime object.
- toString() - An object method returning a string representing this ZonedDateTime object in ISO 8601 date and time format.
Here is an example of toString() output, "2014-04-01T04:41:10.123456789+01:00[Europe/Paris]".
- plus() - An object method returns a copy of this ZonedDateTime object with additional date or time units added.
During the operation, timezone is not changed. ISO 8601 calendar rules and local timezone rules are respected.
- minus() - An object method returns a copy of this ZonedDateTime object with additional date or time units removed.
During the operation, timezone is not changed. ISO 8601 calendar rules and local timezone rules are respected.
- with() - An object method returns a copy of this ZonedDateTime object with a new value set on a specified field
and other fields adjusted accordingly.
During the operation, timezone is not changed. ISO 8601 calendar rules and local timezone rules are respected.
- parse() - A static method returning a ZonedDateTime object representing a moment of time constructed by
parsing the given character sequence using the ISO 8601 date and time format like
"2007-12-03T10:15:30.123+01:00[Europe/Paris]"
- now() - A static method returning a ZonedDateTime object representing the current moment based on the system clock time
and the local timezone.
Table of Contents
About This JDK Tutorial Book
JDK (Java Development Kit)
►Java Date-Time API
What Is Date-Time API
java.time.Instant - Representing a Moment of Time
java.time.Instant Usage Examples
java.time.Instant - get(INSTANT_SECONDS) Error
Converting java.util.Date to java.time.Instant
►java.time.ZonedDateTime - Calendar and Timezone
java.time.ZonedDateTime Usage Examples
Converting java.util.GregorianCalendar to java.time.ZonedDateTime
java.time.OffsetDateTime - Calendar and UTC Offset
java.time.OffsetDateTime Usage Examples
java.time.LocalDateTime - Local Date and time without Timezone
Partial Date and Time Objects and Classes
java.time.format.DateTimeFormatter - Date-Time Strings
java.time.Duration - Time Durations
java.time.Duration Usage Examples
java.time.Period - Periods in Days and Months
java.time.Period Usage Examples
Date, Time and Calendar Classes
Date and Time Object and String Conversion
Number Object and Numeric String Conversion
Locales, Localization Methods and Resource Bundles
Calling and Importing Classes Defined in Unnamed Packages
HashSet, Vector, HashMap and Collection Classes
Character Set Encoding Classes and Methods
Character Set Encoding Maps
Encoding Conversion Programs for Encoded Text Files
Java Logging
Socket Network Communication
Datagram Network Communication
DOM (Document Object Model) - API for XML Files
SAX (Simple API for XML)
DTD (Document Type Definition) - XML Validation
XSD (XML Schema Definition) - XML Validation
XSL (Extensible Stylesheet Language)
Message Digest Algorithm Implementations in JDK
Private key and Public Key Pair Generation
PKCS#8/X.509 Private/Public Encoding Standards
Digital Signature Algorithm and Sample Program
"keytool" Commands and "keystore" Files
KeyStore and Certificate Classes
Secret Key Generation and Management
Cipher - Encryption and Decryption
The SSL (Secure Socket Layer) Protocol
SSL Socket Communication Testing Programs
SSL Client Authentication
HTTPS (Hypertext Transfer Protocol Secure)
Outdated Tutorials
References
Full Version in PDF/EPUB