Java 8 Date-Time API
Java 8 further strengthened the handling of dates and times by introducing a new Date-Time API (JSR 310).
In older versions of Java, the date-time API had several issues, including:
Non-thread-safe - java.util.Date is non-thread-safe, and all date classes are mutable, which is one of the biggest problems with Java's date classes.
Poor design - The definitions of Java's date/time classes were inconsistent, with date classes in both the java.util and java.sql packages, and formatting and parsing classes defined in the java.text package. java.util.Date includes both date and time, while java.sql.Date includes only date, which is not reasonable to include in the java.sql package. Additionally, both classes have the same name, which is a poor design.
Troublesome time zone handling - Date classes did not support internationalization and lacked time zone support, leading Java to introduce java.util.Calendar and java.util.TimeZone classes, which also had all the aforementioned issues.
Java 8 provides many new APIs under the java.time package. Two important ones are:
Local - Simplifies date-time handling without time zone issues.
Zoned - Handles date-time with specified time zones.
The new java.time package covers all operations related to date, time, date/time, time zones, instants, durations, and clocks.
Localized Date-Time API
LocalDate/LocalTime and LocalDateTime classes can be used when time zone is not required. Here is an example:
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 + ", Seconds: " + seconds);
LocalDateTime date2 = currentTime.withDayOfMonth(10).withYear(2012);
System.out.println("Date2: " + date2);
// December 12, 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);
}
}
System.out.println("date5: " + date5);
}
}
Executing the above script, the output is:
$ javac Java8Tester.java
$ java Java8Tester
Current time: 2016-04-15T16:55:48.668
date1: 2016-04-15
Month: APRIL, Day: 15, Second: 48
date2: 2012-04-10T16:55:48.668
date3: 2014-12-12
date4: 22:15
date5: 20:15:30
Date and Time API with Time Zone
If we need to consider time zones, we can use the time zone date and 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 and 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 Zone: " + currentZone);
}
}
Executing the above script, the output is:
$ javac Java8Tester.java
$ java Java8Tester
date1: 2015-12-03T10:15:30+08:00[Asia/Shanghai]
ZoneId: Europe/Paris
Current Zone: Asia/Shanghai