Jdk8 brings new time utility classes, including LocalDateTime, LocalDate, and LocalTime. Here’s a look at how common usage is used on the new utility class.

1. Obtain the current time

LocalDateTime.now()
LocalDate.now()
LocalTime.now()
Copy the code

Getting the current time is as simple as calling the now method, and the code runs as follows

The T16 2019-08-23: was. 439679 2019-08-23 16:17:18. 441228Copy the code

2. Obtain the time of another time zone

1 Obtain the UTC time

LocalDateTime.now(ZoneId.of("UTC"))
LocalDate.now(ZoneId.of("UTC"))
LocalTime.now(ZoneId.of("UTC"))
Copy the code

The result is as follows

The 2019-08-23 T08: all the 2019-08-23 08:20:34 065057. 065973Copy the code

As you can see, it is 8 hours less than the first time. Because Beijing time is 8 east, it is 8 hours more than utc time.

2 Obtain the time of other time zones based on utc

LocalDateTime.now(ZoneId.of("+ 7"))
LocalDate.now(ZoneId.of("+ 7"))
LocalTime.now(ZoneId.of("+ 7"))
Copy the code

Other time zones are easy to obtain according to the UTC standard: + for the East and – for the West;

3. Time formatting and string conversion time

1 Formatting Time

DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now())
DateTimeFormatter.ofPattern("yyyy_MM_dd").format(LocalDate.now())
DateTimeFormatter.ofPattern("HHmmss").format(LocalTime.now())
Copy the code

DateTimeFormatter is a utility class that defines the time format. All three classes can be used, but be careful that LocalDate does not define a time format, and LocalTime does not define a date format, otherwise an error will be reported.

2 Format the string transfer time

LocalDateTime.parse("20190801000000", DateTimeFormatter.ofPattern("yyyyMMddHHmmss"))
Copy the code

4. Time required for creating a vm

LocalDateTime.of(2018, 8, 13, 23, 56, 2)
Copy the code

According to the year, month, day, hour, minute, second can directly create a time, very convenient.

5. Time and timestamp conversion

1. Time to timestamp

LocalDateTime.now().toInstant(ZoneOffset.of("+ 8")).toEpochMilli()
Copy the code

2. Timestamp to time

LocalDateTime.ofInstant(Instant.ofEpochMilli(1566550144837L), ZoneId.systemDefault())
Copy the code

6. Time calculation

1. Get a year, month, day, hour, minute, second property of a certain time

LocalDateTime now = LocalDateTime.now();
now.getYear();
now.getMonth();
now.getDayOfMonth();
now.getDayOfYear();
now.getDayOfWeek();
now.getHour();
now.getMinute();
now.getSecond();
Copy the code

2. Calculate the increase or decrease of a date

LocalDateTime now = LocalDateTime.now(); // Add a year now.plusYears(1); // Reduce a year now.minusyears (1); // Add now.plusmonths (1); // Reduce now.minusmonths (1); .Copy the code

3. Calculate the difference between two dates

LocalDateTime time1 = LocalDateTime.of(2019, 8, 31, 0, 0);
LocalDateTime time2 = LocalDateTime.of(2019, 7, 31, 0, 0);
System.out.println(time1.until(time2,ChronoUnit.DAYS));

result : -31
Copy the code

It’s easy to calculate the number of days between two dates, as well as any other value you want to calculate.

4. Calculate where to leave a little Easter egg

LocalDateTime time2 = LocalDateTime.now().minusDays(1);
LocalDateTime time1 = LocalDateTime.now();
System.out.println(time2.until(time1,ChronoUnit.DAYS));

-----------
LocalDateTime time1 = LocalDateTime.now();
LocalDateTime time2 = LocalDateTime.now().minusDays(1);
System.out.println(time2.until(time1,ChronoUnit.DAYS));
Copy the code

Look at the two calculated results. They are different from what you think.


Returns the directory