The Date, Canlender, and SimpleDateFormat classes have little advantage in front of the new time packet
Date localdate, time localtime, date time localdatetime.
Time zone ZoneID, point Instant, Time unit chronounit, formatting DateTimeFormatter
Localdate
Current date
Localdate localdate = Localdate.now ();
Specified date
// (Mode 1) Localdate localDate1 = Localdate.of (); // (Mode 2) Localdate LocalDate2 = Localdate.parse ("2017-07-25");
Date Plus/minus how many units of time
// plus 1 months (Mode 1) Localdate localdate = localdate.plus (1, chronounit.months); // plus 1 days (Mode 2) Localdate localdate = localdate.plusdays (1);
Parse Date
int dayofmonth = Localdate.getdayofmonth ();
Whether a leap year
boolean b = localdate.isleapyear ();
Whether it is a date before/after
boolean b = Localdate1.isbefore (localDate2); Boolean b = Localdate1.isafter (LOCALDATE2);
Two dates how many time units are different
How many days difference
Long diff = ChronoUnit.DAYS.between (localDate1, localDate2); //difference in how many seconds
long diff = ChronoUnit.SECONDS.between (localDate1, LocalDate2);
Formatting
DateTimeFormatter DateTimeFormatter = Datetimeformatter.ofpattern ("Yyyy-mm-dd"); = Datetimeformatter.format (localdate);
Two dates are equal
Boolean b = localdate1.equals (LOCALDATE2);
LocalTime
Most APIs are the same as localdate, where the difference is
1, cannot judge whether leap year
2.Localdate Object ToString () returns "2017-11-09", LocalTime object ToString () returns "11:16:50.144" (if the number of milliseconds is 0, the last digit is not displayed), If you want to return the HH:MM:SS format, you need to format it.
LocalDateTime
Most APIs are the same as localdate, where the difference is
1, can not directly judge whether leap years, need to first converted to Localdate
Boolean B = localdatetime.tolocaldate (). Isleapyear ();
2,LocalDateTime object ToString () returns "2017-11-09t11:16:50.144", if you want to return the HH:MM:SS format, you need to format.
Working with Java.util.Date objects in legacy code
The date is converted to a Instant object, and then the Localdate, LocalTime, and LocalDateTime respective from factory methods are used as needed to generate the object
// Legacy Code Date birthday = user.getbirthday (); = order.getordertime (); // Conversion Localdate Birthdayex = localdate.from (birthday.toinstant ()); = Localdatetime.from (Ordertime.toinstant ());
Working with Java.util.Calendar objects in legacy code
As with java.util.Date objects, the Calendar object also has a Toinstant method.
Time package common API for Java 8