Java 8 has added LocalDate and LocalTime interfaces. Why do you need to develop a new set of APIs that handle dates and times? Because the old java.util.Date is really hard to use.
java.util.Date month starts from 0, January is 0, December is 11, it’s abnormal! java.time.LocalDate has been changed to enum for both months and week, so it is impossible to use it incorrectly.
Neither java.util.Date nor SimpleDateFormatter are thread-safe, while LocalDate and LocalTime are the same as the most basic String, and are not only thread-safe, but also cannot be modified.
java.util.Date is a "universal interface" that contains dates, time, and milliseconds. If you only want to use java.util.Date to store dates, or only store time, then only you know which parts of the data are useful and which parts of the data are not useful. In the new Java 8, dates and times are clearly divided into LocalDate and LocalTime. LocalDate cannot contain time and LocalTime cannot contain dates. Of course, LocalDateTime can include both date and time.
The reason why the new interface is better is that it takes into account the operation of date and time, and it often happens when pushing forward or backward for a few days. Using java.util.Date with Calendar requires a lot of code to write, and ordinary developers may not be able to write it correctly.
LocalDate
Let's see how to use the new LocalDate:
// Get the current date: LocalDate today = LocalDate.now(); // -> 2014-12-24// Get the date according to the year, month and day, December is 12: LocalDate crisismas = LocalDate.of(2014, 12, 25); // -> 2014-12-25// Get the current date according to the string: LocalDate endOfFeb = LocalDate.parse("2014-02-28"); // Strictly verify according to ISO yyyy-MM-dd, 02 is not allowed to be written as 2. Of course, there is also an overload method that allows you to define the format yourself LocalDate.parse("2014-02-29"); // Invalid date cannot be passed: DateTimeParseException: Invalid date
Date conversion is often encountered, such as:
// Take the first day of this month: LocalDate firstDayOfThisMonth = today.with(TemporalAdjusters.firstDayOfMonth()); // 2014-12-01// Take the second day of this month: LocalDate secondDayOfThisMonth = today.withDayOfMonth(2); // 2014-12-02// Take the last day of this month, and you no longer need to calculate whether it is 28, 29, 30 or 31: LocalDate lastDayOfThisMonth = today.with(TemporalAdjusters.lastDayOfMonth()); // 2014-12-31// Take the next day: LocalDate firstDayOf2015 = lastDayOfThisMonth.plusDays(1); // It becomes 2015-01-01// Take the first Monday in January 2015, and this calculation uses Calendar to kill many brain cells: LocalDate firstMondayOf2015 = LocalDate.parse("2015-01-01").with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY)); // 2015-01-05 LocalTime
LocalTime only contains time. How can I only represent time with java.util.Date before? The answer is, pretend to ignore the date.
LocalTime contains milliseconds:
LocalTime now = LocalTime.now(); // 11:09:09.240
You might want to clear the milliseconds:
LocalTime now = LocalTime.now().withNano(0)); // 11:09:09
The construction time is also very simple:
LocalTime zero = LocalTime.of(0, 0, 0); // 00:00:00
LocalTime mid = LocalTime.parse("12:00:00"); // 12:00:00
Time is also recognized in ISO format, but the following three formats can be identified:
•12:00
•12:01:02
•12:01:02.345
JDBC
The latest JDBC mapping will associate the date type of the database with the new type of Java 8:
SQL -> Java
------------------------------------
date -> LocalDate
time -> LocalTime
timestamp -> LocalDateTime
There will never be any more cases where some parts of the date or time are mapped to java.util.Date where some part of the date or time is 0.
The above is the method of handling dates and time in Java 8 introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!