Introduction to the

In JDK8, three very useful time-related apis have been introduced: Duration, Period, and ChronoUnit.

They are all used for time statistics, and this article will look at the use of these three apis in detail.

Duration

Duration is used to measure the time in seconds and nanoseconds. It is used when time accuracy is required.

Let’s look at the definition of Duration:

public final class Duration
        implements TemporalAmount.Comparable<Duration>, Serializable
Copy the code

As you can see, Duration is a final class and is serializable and comparable. Note that Duration also implements the TemporalAmount interface.

So what is the TemporalAmount interface?

TemporalAmount is the parent interface to Duration and Period.

It defines four methods that must be implemented:

long get(TemporalUnit unit);
List<TemporalUnit> getUnits(a);
Temporal addTo(Temporal temporal);
Temporal subtractFrom(Temporal temporal);
Copy the code

TemporalUnit represents units of time objects, such as years, months, days, hours, minutes and seconds. And Temporal stands for reading and writing to time objects.

Let’s look at some basic operations for Duration:

        Instant start = Instant.parse("The 2020-08-03 T10:3:30. 00 z");
        Instant end = Instant.parse(12 "z" is the 2020-08-03 T10: ticket.);
        Duration duration = Duration.between(start, end);
        log.info("{}",duration.getSeconds());
        log.info("{}",duration.getNano());
        log.info("{}",duration.getUnits());
Copy the code

Above we created two Instant and used the duration. between method to measure the difference between them.

Duration.getseconds () is used to obtain the difference of seconds, and duration.getnano () is used to obtain the difference of sub-seconds.

Finally, we’ll use duration.getunits () to take a look at temporalUnits supported by Duration.

Take a look at the execution result:

 INFO com.flydean.time - 60
 INFO com.flydean.time - 120000000
 INFO com.flydean.time - [Seconds, Nanos]
Copy the code

In addition to Instance, we can also use LocalTime:

        LocalTime start2 = LocalTime.of(1.20.25.1314);
        LocalTime end2 = LocalTime.of(3.22.27.1516);
        Duration.between(start2, end2).getSeconds();
Copy the code

We can also do plus and minus for Duration, and use isNegative to determine the order of the two times:

duration.plusSeconds(60);
duration.minus(30, ChronoUnit.SECONDS);
log.info("{}",duration.isNegative());
Copy the code

In addition, we can easily create Duration by using the duration. of method:

Duration fromDays = Duration.ofDays(1);
Duration fromMinutes = Duration.ofMinutes(60);
Copy the code

Period

The units of Period are year, month, and day.

The operation is basically the same as Duration.

Let’s start with the definition:

public final class Period
        implements ChronoPeriod.Serializable 
Copy the code

ChronoPeriod is a sub-interface of TemporalAmount.

Similarly, we can construct a Period from LocalDate using period. between:

        LocalDate startDate = LocalDate.of(2020.2.20);
        LocalDate endDate = LocalDate.of(2021.1.15);

        Period period = Period.between(startDate, endDate);
        log.info("{}",period.getDays());
        log.info("{}",period.getMonths());
        log.info("{}",period.getYears());
Copy the code

It can also be constructed directly from period. of:

Period fromUnits = Period.of(3.10.10);
        Period fromDays = Period.ofDays(50);
        Period fromMonths = Period.ofMonths(5);
        Period fromYears = Period.ofYears(10);
        Period fromWeeks = Period.ofWeeks(40);
Copy the code

Finally, we can use plus or minus:

period.plusDays(50);
period.minusMonths(2);
Copy the code

ChronoUnit

ChronoUnit is used to represent units of time, but also provides some very useful between methods for calculating the difference between two times:

        LocalDate startDate = LocalDate.of(2020.2.20);
        LocalDate endDate = LocalDate.of(2021.1.15);
        long years = ChronoUnit.YEARS.between(startDate, endDate);
        long months = ChronoUnit.MONTHS.between(startDate, endDate);
        long weeks = ChronoUnit.WEEKS.between(startDate, endDate);
        long days = ChronoUnit.DAYS.between(startDate, endDate);
        long hours = ChronoUnit.HOURS.between(startDate, endDate);
        long minutes = ChronoUnit.MINUTES.between(startDate, endDate);
        long seconds = ChronoUnit.SECONDS.between(startDate, endDate);
        long milis = ChronoUnit.MILLIS.between(startDate, endDate);
        long nano = ChronoUnit.NANOS.between(startDate, endDate);
Copy the code

Example for this article: learn-Java-base-9-to-20

Author: Flydean program stuff

Link to this article: www.flydean.com/duration-pe…

Source: Flydean’s blog

Welcome to pay attention to my public number: “procedures those things” the most popular interpretation, the most profound dry goods, the most concise tutorial, many you do not know the small skills you find!