Java 8 Date-Time API
Java 8 further strengthens the handling of dates and times by releasing the new Date-Time API (JSR 310).
In older versions of Java, the date-time API had many problems, including:
-
Not Thread-Safe − java.util.Date is not thread-safe, and all date classes are mutable; this is one of the biggest problems with Java date classes.
-
Poor Design − The definitions of Java's date/time classes are inconsistent; date classes exist in both the java.util and java.sql packages, and the classes used for formatting and parsing are defined in the java.text package. java.util.Date contains both date and time, while java.sql.Date contains only a date, so including it in the java.sql package is unreasonable. In addition, both classes have the same name, which is itself a very bad design.
-
Troublesome Time Zone Handling − The date classes do not provide internationalization or time zone support, so Java introduced the java.util.Calendar and java.util.TimeZone classes, but they have all the same problems described above.
Java 8 provides many new APIs under the java.time package. The following are two relatively important APIs:
-
Local − Simplifies date-time handling without time zone issues.
-
Zoned (time zone) − Handles date-time through a specified time zone.
The new java.time package covers all operations for handling dates, times, date/times, time zones, instants, durations (during), and clocks.
Localized Date-Time API
The LocalDate/LocalTime and LocalDateTime classes can be used when time zone handling is not necessary. The code is as follows:
Java8Tester.java file
import
java
.
time
.
LocalDate
;
import
java
.
time
.
LocalTime
;
import
java
.
time
.
LocalDateTime
;
import
java
.
time
.
Month
;
public
class
Java8Tester
{
public
static
void
main
(
String
args
[
]
)
{
Java8Tester
java8tester
=
new
Java8Tester
(
)
;
java8tester
.
testLocalDateTime
(
)
;
}
public
void
testLocalDateTime
(
)
{
//
Get the current date-time
LocalDateTime
currentTime
=
LocalDateTime
.
now
(
)
;
System
.
out
.
println
(
"
Current time:
"
currentTime
)
;
LocalDate
date1
=
currentTime
.
toLocalDate
(
)
;
System
.
out
.
println
(
"
date1:
"
date1
)
;
Month
month
=
currentTime
.
getMonth
(
)
;
int
day
=
currentTime
.
getDayOfMonth
(
)
;
int
seconds
=
currentTime
.
getSecond
(
)
;
System
.
out
.
println
(
"
Month:
"
month
"
, day:
"
day
"
, second:
"
seconds
)
;
LocalDateTime
date2
=
currentTime
.
withDayOfMonth
(
10
)
.
withYear
(
2012
)
;
System
.
out
.
println
(
"
date2:
"
date2
)
;
//
12 december 2014
LocalDate
date3
=
LocalDate
.
of
(
2014
,
Month
.
DECEMBER
,
12
)
;
System
.
out
.
println
(
"
date3:
"
date3
)
;
//
22 hours 15 minutes
LocalTime
date4
=
LocalTime
.
of
(
22
,
15
)
;
System
.
out
.
println
(
"
date4:
"
date4
)
;
//
Parse a string
LocalTime
date5
=
LocalTime
.
parse
(
"
20:15:30
"
)
;
System
.
out
.
println
(
"
date5:
"
date5
)
;
}
}
Executing the above script produces the following output:
$ javac Java8Tester.java
$ java Java8Tester
当前时间: 2016-04-15T16:55:48.668
date1: 2016-04-15
月: APRIL, 日: 15, 秒: 48
date2: 2012-04-10T16:55:48.668
date3: 2014-12-12
date4: 22:15
date5: 20:15:30
Using the Time Zone Date-Time API
If we need to consider time zones, we can use the time zone date-time API:
Java8Tester.java file
import
java
.
time
.
ZonedDateTime
;
import
java
.
time
.
ZoneId
;
public
class
Java8Tester
{
public
static
void
main
(
String
args
[
]
)
{
Java8Tester
java8tester
=
new
Java8Tester
(
)
;
java8tester
.
testZonedDateTime
(
)
;
}
public
void
testZonedDateTime
(
)
{
//
Get the current date-time
ZonedDateTime
date1
=
ZonedDateTime
.
parse
(
"
2015-12-03T10:15:30+05:30[Asia/Shanghai]
"
)
;
System
.
out
.
println
(
"
date1:
"
date1
)
;
ZoneId
id
=
ZoneId
.
of
(
"
Europe/Paris
"
)
;
System
.
out
.
println
(
"
ZoneId:
"
id
)
;
ZoneId
currentZone
=
ZoneId
.
systemDefault
(
)
;
System
.
out
.
println
(
"
Current time zone:
"
currentZone
)
;
}
}
Executing the above script produces the following output:
$ javac Java8Tester.java
$ java Java8Tester
date1: 2015-12-03T10:15:30+08:00[Asia/Shanghai]
ZoneId: Europe/Paris
当期时区: Asia/Shanghai
Java 8 New Features