Legacy API defect
1. Design Defect: Calendar class such as the month is counted from 0
2. Non-thread-safe: java.util.Date non-thread-safe, all the dates are variable class
3. Time zone trouble handling: Date class does not offer international, no time zone support, so the introduction of the Java class java.util.Calendar and java.util.TimeZone, but above all their problems also exist
The new API
1.localdat to, localtime
LocalDate: instances of the class is an immutable object, it only provides a simple date, time does not include the information of the day, it does not carry any information related to the time zone. LocalDate can create an instance of (y, m, d) by the static method, may be () Gets the current date by now.
LocalTime: time of day, such as 8:55:13, can be expressed LocalTime. LocalTime may be used to create instances of method overloading of the plant
LocalDateTime: LocalDate and LocalTime is fit, also said the date and time, but with no time zone information
Code Example:
package Date; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.Month; public class the Test { public static void main (String [] args) { // get the current date and time the 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 + ", 日: " + day + ", 秒: " + 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 is, 15 ); System.out.println("date4: " + date4); // parse the string LocalTime date5 = LocalTime.parse ( "20:15:30" ); System.out.println("date5: " + date5); } }
2. manipulation, parsing and formatting date
All dates and times are to achieve a Temporal Object Interface, static or instance method creates a copy of an object without modifying the original object. Common methods are as follows:
Method name | Whether it is a static method | description |
from | Yes | Create an object instance of the object passed in Temporal |
now | Yes | Temporal create objects based on the system clock |
of | Yes | The object is created by an armed Temporal instance of an object |
parents | Yes | Temporal objects created by the string instance |
atOffset | no | The Temporal objects and a combination of the time zone offset |
atZone | no | The Temporal objects and a combination of time zone |
format | no | Temporal format used to convert the object to a string |
get | no | Temporal value read for a part of the object |
minus | no | The copy is created by the current value of the Temporal object minus a certain length of time |
plus | no | Plus a certain length of time to create the copy by the current value of the Temporal object |
with | no | The Temporal objects to create the object as a template for some state modified copy |
3. Use TemporalAdjuster.
It can be used with heavy-duty version of the method, passing it provides a more customized selection of TemporalAdjuster object.
package Date; import java.time.DayOfWeek; import java.time.LocalDateTime; import java.time.temporal.TemporalAdjusters; public class TemporalAdjustersTest { public static void main (String [] args) { // Current time: 2020-02-21T16: 32: 01.492 the LocalDateTime = currentTime LocalDateTime.now (); System.out.println ( "Current Time:" + currentTime); // nextDay: 2020-02-23T16: 32: 01.492 create a new date, and set its value to adjust the date, first date a few weeks in line with the requirements specified. If the date has been met, the object is returned directly the LocalDateTime nextDay = currentTime.with (TemporalAdjusters.nextOrSame (DayOfWeek.SUNDAY)); System.out.println("nextDay: " + nextDay); // firstDay: 2020-02-01T16: 37 [: 38.789 create a date, it is the first day of the month the LocalDateTime firstDay = currentTime.with (TemporalAdjusters.firstDayOfMonth ()); System.out.println("firstDay: " + firstDay); } }