• What’s in the Java8 DateTime API
    • LocalDate
    • Convert LocalDate to Date
    • LocalTime
    • LocalDateTime
    • LocalDateTime and Date
    • Instant
    • ZoneId
    • date

Java’s date and time API has been a bit of a headache.

Date = java.util.Date = java.sql.Date = java.util.Date = java.util.Date = java.sql.Date

The new Date(year,month,day) was marked as @deprecated and will be Deprecated in the future. It was Deprecated in jdk1.1 and is now Deprecated in java9.

Well, given a Calendar, every time an operation, feel, IDE set a line of 80 characters, is really not enough.

I want to make a pure date, that is a trouble, I believe many people write:

Calendar cal=Calendar.getInstance();
cal.set(Calendar.HOUR,0);
cal.set(Calendar.MINUTE,0);
cal.set(Calendar.SECOND,0);
Date date=cal.getTime();Copy the code

Stuffed heart ~

It may seem like Calendar is a bit better than Date, but it has inherited a nasty feature of Date, the month problem.

System.out.println(cal.getTime()); //Fri Nov 17 12:00:00 CST 2017 System.out.println(cal.get(Calendar.MONTH)); / / 10Copy the code

Java8 has finally changed, introducing a joda-time API, although it is still not as useful as joda.

What’s in the Java8 DateTime API?

The date-time API separates date and time into LocalDate and LocalTime;

LocalDateTime is used when both date and time are present;

Improved JDK TimeZone with more elegant ZoneId;

Added the concept of Instant, which can be understood as a moment on the timeline;

LocalDate

LocalDate date1= localdate.now (); System.out.println(date1); Date2 = localdate. of(2017, 12, 24); System.out.println(date2); / / to get on the first day of the month date LocalDate minus = date1, minus (date1. GetDayOfMonth () - 1, ChronoUnit.. DAYS); System.out.println(minus);Copy the code

Convert LocalDate to Date

//LocalDate -> Date LocalDate localDate1=LocalDate.now(); ZoneId zoneId=ZoneId.systemDefault(); Instant instant=localDate1.atStartOfDay(zoneId).toInstant(); Date from = Date.from(instant); System.out.println(from); Date2 =new Date(); Instant instant = date2.toInstant(); ZoneId zoneId=ZoneId.systemDefault(); LocalDate localDate2 = instant.atZone(zoneId).toLocalDate(); System.out.println(localDate);Copy the code

LocalTime

In fact, there are not as many scenarios using only time as there are using only dates. In addition, the idea of LocalDate is clear, and the use of LocalTime is similar, and I will add it later.

LocalDateTime

A combination of LocalDate and LocalTime, which is a traditional date-like time.

LocalDate localDate = dt.toLocalDate();
LocalTime localTime = dt.toLocalTime();Copy the code

LocalDateTime and Date

Date = new Date(); Instant instant = date.toInstant(); ZoneId zone = ZoneId.systemDefault(); LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone); LocalDateTime = localDatetime.now (); ZoneId zone = ZoneId.systemDefault(); Instant instant = localDateTime.atZone(zone).toInstant(); Date date = Date.from(instant);Copy the code

Instant

As you can see from the previous format conversion.

Date -> Instant -(ZoneId)-> LocalDate

With text description, that is, Date can be converted to Instant; Instant can be converted to LocalDate with ZoneId. And vice versa.

ZoneId

ZoneId zoneId=ZoneId.systemDefault();
System.out.println(zoneId);
TimeZone tz=TimeZone.getDefault();
System.out.println(tz);Copy the code

Output:

Asia/Shanghai sun.util.calendar.ZoneInfo[id="Asia/Shanghai",offset=28800000,dstSavings=0,useDaylight=false,transitions=19,lastRule=nul l]Copy the code

It’s clearer what information ZoneId ignores that isn’t useful. If necessary, these can be obtained from the property zoneid.getrules ().

date

The introduction of the Joda-time feature in date calculation is very convenient.

For example, to get the date of the first day of the month, we used to need to do this.

Calendar cal=Calendar.getInstance();
cal.set(Calendar.HOUR,0);
cal.set(Calendar.MINUTE,0);
cal.set(Calendar.SECOND,0);

cal.set(Calendar.DAY_OF_MONTH,1);
System.out.println(cal.getTime());Copy the code

How about now?

LocalDate date=LocalDate.now().withDayOfMonth(1);
System.out.println(date);Copy the code

If we wanted to calculate the moment before the current time, we used to do this:

Calendar cal=Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH, -1);Copy the code

How about now?

LocalDateTime date=LocalDateTime.now().minusDays(1);Copy the code

It’s both simpler and semantically better. Support chain syntax, easy coding.

My blog will soon enter the “cloud community”, we sincerely invite technical colleagues to join us.