Original: http://www.liaoxuefeng.com/article/00141939241051502ada88137694b62bfe844cd79e12c32000
Reference: http://www.cnblogs.com/feika/p/4448924.html (more details)
Java 8 new LocalDate and LocalTime interface, why do you have a new set of processing date and time API? Because the old java.util.Date really is too difficult to use.
java.util.DateMonth from 0 start, January yes 0 , December Yes 11 , pervert bar! The java.time.LocalDate month and the week are changed enum , it is impossible to use the wrong again.
java.util.DateAnd SimpleDateFormatter are not thread-safe, and LocalDate LocalTime as with the most basic String , are immutable types, not only thread-safe, but cannot be modified.
java.util.Dateis a "universal interface", it contains the date, time, and the number of milliseconds, if you only want to java.util.Date store the date, or only to store time, then only you know which parts of the data is useful, which part of the data is not available. In the new Java 8, dates and times are clearly divided into LocalDate and LocalTime , LocalDate cannot contain time, LocalTime cannot contain dates. Of course, LocalDateTime you can include both date and time.
The reason the new interface is better is that it takes a date-time operation and often pushes forward or pushes backwards for a few days. With java.util.Date mates Calendar to write a lot of code, and the general developers are not necessarily able to write right.
Localdate
Look at the new LocalDate how to use:
// 取当前日期:LocalDate today = LocalDate.now(); // -> 2014-12-24// 根据年月日取日期,12月就是12:LocalDate crischristmas = LocalDate.of(2014, 12, 25); // -> 2014-12-25// 根据字符串取:LocalDate endOfFeb = LocalDate.parse("2014-02-28"); // 严格按照ISO yyyy-MM-dd验证,02写成2都不行,当然也有一个重载方法允许自己定义格式LocalDate.parse("2014-02-29"); // 无效日期无法通过:DateTimeParseException: Invalid date
Date conversions are often encountered, such as:
Take the 1th day of the month: localdate Firstdayofthismonth = today.With (Temporaladjusters.firstdayofmonth ()); //2014-12-01//Take the 2nd day of this month: localdate seconddayofthismonth = Today.withdayofmonth (2); //2014-12-02//Take the last day of the month, no longer calculate whether it is 28,29,30 or 31:localdate lastdayofthismonth = today. With (Temporaladjusters.lastdayofmonth ()); //2014-12-31//Remove day: localdate firstDayOf2015 = lastdayofthismonth.plusdays (1); //become 2015-01-01//Take the first Monday of January 2015, this calculation uses calendar to die a lot of brain cells: localdate firstMondayOf2015 = Localdate.parse ( "2015-01-01"). With (Temporaladjusters.firstinmonth (dayofweek.monday)); //2015-01-05
LocalTime
LocalTimeIt only contains time, java.util.Date how can it be used to represent time? The answer is to 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 simple:
LocalTime zero = LocalTime.of(0, 0, 0); // 00:00:00LocalTime mid = LocalTime.parse("12:00:00"); // 12:00:00
Time is also recognized in ISO format, but the following 3 formats are recognized:
- 12:00
- 12:01:02
- 12:01:02.345
Jdbc
The latest JDBC Mapping associates the date type of the database with the new type of Java 8:
SQL -> Java--------------------------date -> LocalDatetime -> LocalTimetimestamp -> LocalDateTime
There is no longer a java.util.Date case that maps to some part of the date or time 0 .
How to handle dates and times happily in Java 8