How to handle dates and times happily in Java 8
Original article: http://www.liaoxuefeng.com/article/00141939241051502ada88137694b62bfe844cd79e12c32000
Reference: http://www.cnblogs.com/feika/p/4448924.html (more details)
Added Java 8LocalDateAndLocalTimeInterface. Why do I need a new set of APIS for processing dates and times? Because the oldjava.util.DateIt is too difficult to use.
java.util.DateMonth From0Starting from January0, December is11Abnormal!java.time.LocalDateThe month and week have been changedenum, It is impossible to use the error again.
java.util.DateAndSimpleDateFormatterNot thread-safe,LocalDateAndLocalTimeAnd the most basicStringSimilarly, it is of the same type, not only thread security, but also cannot be modified.
java.util.DateIs a "universal interface", which contains the date, time, and number of milliseconds. If you only want to usejava.util.DateIf you store a date or only a time, only you know which part of the data is useful and which part of the data is unavailable. In the new Java 8, date and time are clearly dividedLocalDateAndLocalTime,LocalDateTime cannot be included,LocalTimeDate cannot be included. Of course,LocalDateTimeTo include both the date and time.
The reason why the new interface is more useful is that the date and time operations are taken into account, often occur to push forward or push back for a few days. Usejava.util.DateCooperationCalendarA lot of code is required, and developers may not be able to write the code correctly.
LocalDate
Look at the newLocalDateHow to use:
// Obtain the current date: LocalDate today = LocalDate. now (); //-> 2014-12-24 // obtain the date based on the year, month, and day. January 1, December is 12: LocalDate crichristmas = LocalDate. of (2014, 12, 25); //> 2014-12-25 // obtain according to the string: LocalDate endOfFeb = LocalDate. parse (""); // strictly follow the ISO yyyy-MM-dd verification. 02 cannot be written as 2. Of course, there is also a overload method that allows you to define the format LocalDate. parse (""); // Invalid date cannot pass: DateTimeParseException: Invalid date
Date conversion is often encountered, such:
// Obtain the 1st day of the month: LocalDate firstDayOfThisMonth = today. with (TemporalAdjusters. firstDayOfMonth (); // 2014-12-01 // the first day of the month: LocalDate secondDayOfThisMonth = today. withDayOfMonth (2); // 2014-12-02 // obtain the last day of the month, and do not calculate whether it is 28,29, 30 or 31: LocalDate lastDayOfThisMonth = today. with (TemporalAdjusters. lastDayOfMonth (); // 2014-12-31 // delete a day: LocalDate firstDayOf2015 = lastDayOfThisMonth. plusDays (1); // It is changed to 2015-01-01 // take the first Monday of June January 2015. This computation uses Calendar to die many brain cells: LocalDate firstMondayOf2015 = LocalDate. parse ("2015-01-01 "). with (TemporalAdjusters. firstInMonth (DayOfWeek. MONDAY ); //
LocalTime
LocalTimeContains only the time. Previously Usedjava.util.DateHow can we only represent the time? The answer is, pretend to ignore the date.
LocalTimeContains milliseconds:
LocalTime now = LocalTime.now(); // 11:09:09.240
You may want to clear the number of 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:00LocalTime mid = LocalTime.parse("12:00:00"); // 12:00:00
The time is also identified according to the ISO format, but the following three formats can be identified:
- 12: 00
- 12:01:02
- 12:01:02. 345
JDBC
The latest JDBC ing will associate the date type of the database with the new type of Java 8:
SQL -> Java--------------------------date -> LocalDatetime -> LocalTimetimestamp -> LocalDateTime
No longer mapsjava.util.DateSome part of the date or time is0.