Java8 Datetime Api
[ Java 8 New Features](#)\\n\\n* * *\\n\\nJava 8 introduces a new Date-Time API (JSR 310) to further strengthen the handling of date and time.\\n\\nIn the older version of Java, the date and time API has many problems, including:\\n\\n* **Non-thread-safe** β java.util.Date is not thread-safe, all date classes are mutable, which is one of the biggest problems with Java's date classes.\\n\\n* **Poor design** β Java's date/time class definitions are not consistent. There are date classes in both java.util and java.sql packages, and the classes 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 date. Placing it in the java.sql package is not reasonable. Additionally, both classes have the same name, which is itself a very poor design.\\n\\n* **Troublesome time zone handling** β The date classes do not provide internationalization and no time zone support. Therefore, Java introduced java.util.Calendar and java.util.TimeZone classes, but they have all the same problems mentioned above.\\n\\nJava 8 provides many new APIs under the **java.time** package. The following are two relatively important APIs:\\n\\n* **Local** β Simplifies date and time processing without time zone issues.\\n\\n* **Zoned** β Handles date and time through specified time zones.\\n\\nThe new java.time package covers all operations for handling date, time, date/time, time zone, instants, duration, and clock.\\n\\n* * *\\n\\n## Localized Date and Time API\\n\\nThe LocalDate/LocalTime and LocalDateTime classes can be used when handling time zones is not required. The code is as follows:\\n\\n## Java8Tester.java File\\n\\nimport 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(){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); LocalDate date3 = LocalDate.of(2014, Month.DECEMBER, 12); System.out.println("date3: " + date3); LocalTime date4 = LocalTime.of(22, 15); System.out.println("date4: " + date4); LocalTime date5 = LocalTime.parse("20:15:30"); System.out.println("date5: " + date5); }}\\n\\nExecute the above script, the output is:\\n\\n$ javac Java8Tester.java $ java Java8TesterCurrent time: 2016-04-15T16:55:48.668 date1: 2016-04-15Month: APRIL, Day: 15, Second: 48 date2: 2012-04-10T16:55:48.668 date3: 2014-12-12 date4: 22:15 date5: 20:15:30\\n\\n* * *\\n\\n## Date and Time API Using Time Zones\\n\\nIf we need to take time zones into account, we can use the date and time API with time zones:\\n\\n## Java8Tester.java File\\n\\nimport 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(){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); }}\\n\\nExecute the above script, the output is:\\n\\n$ javac Java8Tester.java $ java Java8Tester date1: 2015-12-03T10:15:30+08:00[Asia/Shanghai]ZoneId: Europe/ParisCurrent Time Zone: Asia/Shanghai\\n\\n* * Java 8 New Features](#)
YouTip