Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the new time API:Duration Period and ChronoUnit in JDK8

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to understand the new time API:Duration Period and ChronoUnit in JDK8". In daily operation, I believe many people have doubts about how to understand the new time API:Duration Period and ChronoUnit in JDK8. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to understand the new time API:Duration Period and ChronoUnit in JDK8"! Next, please follow the editor to study!

Duration

Duration is mainly used to measure the time of seconds and nanoseconds, and it is used in situations where high time precision is required.

Let's take a look at the definition of Duration:

Public final class Duration implements TemporalAmount, Comparable, Serializable

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

So what is the TemporalAmount interface?

TemporalAmount is the parent interface of Duration and Period.

It defines four methods that must be implemented:

Long get (TemporalUnit unit); List getUnits (); Temporal addTo (Temporal temporal); Temporal subtractFrom (Temporal temporal)

TemporalUnit represents the unit of time object, such as years, months, days, hours, minutes and seconds. Temporal represents the read and write operation of the time object.

Let's take a look at some basic operations of Duration:

Instant start = Instant.parse ("2020-08-03T10:15:30.00Z"); Instant end = Instant.parse ("2020-08-03T10:16:30.12Z"); Duration duration = Duration.between (start, end); log.info ("{}", duration.getSeconds ()); log.info ("{}", duration.getNano ()); log.info ("{}", duration.getUnits ())

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

The difference in the second part is obtained by using duration.getSeconds (), while the difference in the precision part below the second is obtained by using duration.getNano ().

Finally, let's use duration.getUnits () to take a look at the TemporalUnit (time unit) 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]

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 ()

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

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

In addition, we can easily use the Duration.of method to create a Duration:

Duration fromDays = Duration.ofDays (1); Duration fromMinutes = Duration.ofMinutes (60); Period

The units of Period are year, month and day.

The operation is basically the same as Duration.

Let's first look at the definition:

Public final class Period implements ChronoPeriod, Serializable

Where ChronoPeriod is a subinterface of TemporalAmount.

Similarly, we can use Period.between to build Period from LocalDate:

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 ())

You can also build 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)

Finally, we can use the operation of plus or minus:

Period.plusDays (50); period.minusMonths (2); ChronoUnit

ChronoUnit is used to represent time units, but it also provides some very useful between methods to calculate 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). At this point, the study on "how to understand the new time API:Duration Period and ChronoUnit in JDK8" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report