Java 8 Date and Time API
Java added a new Date and Time API in Java 8 version that consists of several classes, interfaces, and enum to handle the date and time.
Before Java 8, There was a java.util.Date
class to create and handle date and time that has limitations like Not thread-safe, Poor design, and Difficult time zone handling.
The new Date and Time API is based on the ISO calendar system and all the classes are immutable and thread-safe.
The classes of java.time package represent the principle date-time concepts, including instants, durations, dates, times, time-zones, and periods.
Java Date Time API Classes
The new java.time package includes the various classes and enums that are tabled below.
Class
|
Description
|
Clock
|
A clock providing access to the current instant, date, and time using a time-zone.
|
Duration
|
A time-based amount of time, such as '34.5 seconds'.
|
Instant
|
An instantaneous point on the time-line.
|
LocalDate
|
A date without a time-zone in the ISO-8601 calendar system, such as 2007-12-03.
|
LocalDateTime
|
A date-time without a time-zone in the ISO-8601 calendar system, such as 2007-12-03T10:15:30.
|
LocalTime
|
A time without a time-zone in the ISO-8601 calendar system, such as 10:15:30.
|
MonthDay
|
A month-day in the ISO-8601 calendar system, such as --12-03.
|
OffsetDateTime
|
A date-time with an offset from UTC/Greenwich in the ISO-8601 calendar system, such as 2007-12-03T10:15:30+01:00.
|
OffsetTime
|
A time with an offset from UTC/Greenwich in the ISO-8601 calendar system, such as 10:15:30+01:00.
|
Period
|
A date-based amount of time in the ISO-8601 calendar system, such as '2 years, 3 months and 4 days'.
|
Year
|
A year in the ISO-8601 calendar system, such as 2007.
|
YearMonth
|
A year-month in the ISO-8601 calendar system, such as 2007-12.
|
ZonedDateTime
|
A date-time with a time-zone in the ISO-8601 calendar system, such as 2007-12-03T10:15:30+01:00 Europe/Paris.
|
ZoneId
|
A time-zone ID, such as Europe/Paris.
|
ZoneOffset
|
A time-zone offset from Greenwich/UTC, such as +02:00.
|
Java 8 Date Time API Enum
The following are the Enum present in java.time package.
Enum |
Description |
DayOfWeek
|
A day-of-week, such as 'Tuesday'.
|
Month
|
A month-of-year, such as 'June'.
|
Example: LocalDate Class
This class is available in java.time package. It represents a date without a time-zone in the ISO-8601 calendar system, such as 2007-12-03.
The LocalDate
class is an immutable date-time object that is used to create a locale date. See the below example.
public class STDemo {
public static void main(String[] args){
LocalDate date = LocalDate.parse("2012-12-10");
System.out.println(date);
}
}
2012-12-10
Example: LocalDateTime Class
This class is used to create a date with time but without a time-zone. It represents a date-time without a time-zone in the ISO-8601 calendar system, such as 2010-10-03T10:10:50.
The LocalDateTime
class is an immutable date-time object that represents a date-time. We can use it to represent a date with time. For example, the value "2nd October 2007 at 13:45.30.123456789" can be stored in a LocalDateTime
.
import java.time.LocalDateTime;
public class STDemo {
public static void main(String[] args){
LocalDateTime date = LocalDateTime.of(2012, 12, 10, 2, 30);
System.out.println(date);
}
}
2012-12-10T02:30
Example: LocalTime Class
This class is used to create a local time object. It represents a time without a time-zone in the ISO-8601 calendar system, such as 11:10:30.
The LocalTime
class is an immutable date-time object that represents a time, For example, the value "13:45.30.123456789" can be stored in a LocalTime
. See the below example.
import java.time.LocalTime;
public class STDemo {
public static void main(String[] args){
LocalTime time = LocalTime.of(12, 12,30);
System.out.println(time);
}
}
12:12:30
Example: ZonedDateTime Class
This class is used to create a date-time object with a time-zone. It represents a date-time with a time-zone in the ISO-8601 calendar system, such as 2017-10-03T10:10:30+05:30 Asia/Kolkata.
The ZonedDateTime
class is an immutable representation of a date-time with a time-zone. This class stores all date and time fields. For example, the value "2nd October 2017 at 15:40.35.123456786 +05:30 in the Asia/Kolkata time-zone" can be stored in a ZonedDateTime
.
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class STDemo {
public static void main(String[] args){
ZonedDateTime time = ZonedDateTime.of(2015, 10, 15, 2, 10, 30, 0, ZoneId.systemDefault());
System.out.println(time);
}
}
2015-10-15T02:10:30+05:30[Asia/Kolkata]
Example: OffSetDateTime Class
This class is used to create a date-time with an offset from UTC/Greenwich in the ISO-8601 calendar system, such as 2017-10-03T15:10:30+01:00.
The OffsetDateTime
class is an immutable representation of a date-time with an offset. This class stores all date and time fields, as well as the offset from UTC/Greenwich. For example, the value "2nd October 2017 at 15:42.35.123456789 +02:00" can be stored in an OffsetDateTime
.
import java.time.OffsetDateTime;
public class STDemo {
public static void main(String[] args){
OffsetDateTime date = OffsetDateTime.parse("2017-02-03T12:30:30+01:00");
System.out.println(date);
}
}
2017-02-03T12:30:30+01:00
Example: OffsetTime Class
This class is used to store time with an offset from UTC/Greenwich in the ISO-8601 calendar system, such as 15:10:35+01:00.
The OffsetTime
class is an immutable date-time object that represents a time. This class stores all time fields, as well as a zone offset. For example, the value "15:40.35.123456789+02:00" can be stored in an OffsetTime
.
import java.time.OffsetTime;
public class STDemo {
public static void main(String[] args){
OffsetTime time = OffsetTime.parse("12:30:30+01:00");
System.out.println(time);
}
}
12:30:30+01:00