Java 8 further enhances the processing of date and time by issuing a new Date-Time API (JSR 310).
In the old version of Java, there are many problems with the date-time API, including:
- Non-thread-safe - java.util.Date is non-thread-safe and all date classes are mutable. This is one of the biggest problems with Java date classes.
- Poor design - The definition of Java's date/time classes is inconsistent. There are date classes in the java.util and java.sql packages, and classes used for formatting and parsing are defined in the java.text package. java.util.Date contains both the date and time, and java.sql.Date only contains the date. Including it in the java.sql package is not reasonable. In addition, both classes have the same name, which is a very bad design in itself.
- Time Zone Handling Trouble - The date class doesn't provide internationalization, there is no time zone support, so Java introduces the java.util.Calendar and java.util.TimeZone classes, but they also have all the above problems.
Java 8 provides many new APIs under the java.time package. The following are two more important APIs:
- Local − simplifies the processing of date and time without the problem of time zones.
- Zoned − The date and time are processed by the time zone.
The new java.time package covers all processing date, time, date/time, time zone, instants, duration, and clock operations.
Localized Datetime API
The LocalDate/LocalTime and LocalDateTime classes may not be necessary when dealing with time zones. code show as below:
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 datetime
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);
// 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 the string
LocalTime date5 = LocalTime.parse("20:15:30");
System.out.println("date5: " + date5);
}
}
Execute the above script, the output is:
$ javac Java8Tester.java $ java Java8Tester current time: 2016-04-15T16:55:48.668 date1: 2016-04-15 months: APRIL, date: 15, seconds: 48 date2: 2012-04-10T16:55:48.668 date3: 2014-12-12 date4: 22:15 date5: 20:15:30
Time zone API using time zone
If we need to consider the time zone, we can use the datetime API of the time zone:
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 time zone: " + currentZone);
}
}
Execute 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 Timezone: Asia/Shanghai
Comments
Post a Comment