Get the App
SLTechnology News&Howtos  ›  Servers  › 

How does java.time.Duration understand?

Shulou Source: shulou.com Published: 2022-05-31 23:34:39 09月27日 Update

This article mainly explains "how to understand java.time.Duration". Friends who are interested might as well take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to understand java.time.Duration.

A Duration object represents a period of time between two Instant and is a new feature added in Java 8.

An Duration instance is immutable, and its value cannot be changed after the object is created. You can only use the Duration calculation method to create a new Durtaion object. You'll see it later in the tutorial.

Create a Duration instance

Using the factory method of the Duration class to create a Duration object, here is an example of using between ():

Instant first = Instant.now ()

/ / wait some time while something happens

Instant second = Instant.now ()

Duration duration = Duration.between (first, second)

Time to access Duration

There are two fields in a Duration object: the nanosecond value (the part that is less than one second) and the second value (a total of several seconds), and their combination represents the length of time. Note that unlike when using System.getCurrentTimeMillis (), Duration does not contain the millisecond attribute.

You can get their values in two ways:

Long seconds = getSeconds ()

Int nanos = getNano ()

You can also convert the whole time to other units such as nanoseconds, minutes, hours, days:

ToNanos ()

ToMillis ()

ToMinutes ()

ToHours ()

ToDays ()

For example, toNanos () differs from getNano () in that toNanos () gets the total number of nanoseconds of Duration, while getNano () only gets less than one second of that time. You may ask why there is no toSeconds () method, because there is already a getSeconds () method that can achieve the same function.

Duration calculation

The Duration class contains a series of calculation methods:

PlusNanos ()

PlusMillis ()

PlusSeconds ()

PlusMinutes ()

PlusHours ()

PlusDays ()

MinusNanos ()

MinusMillis ()

MinusSeconds ()

MinusMinutes ()

MinusHours ()

MinusDays ()

What these methods do is similar, and I won't show the details of the internal implementation here, just show an example of addition and subtraction:

Duration start =... / / obtain a start duration

Duration added = start.plusDays (3)

Duration subtracted = start.minusDays (3)

The first line creates a Duration object called start. You can refer to the previous code for how to create it. The second and third lines of the sample create two new Duration. By calling the addition and subtraction of start, the representation time of the added object is three days longer than that of start, while the substracted is three days less.

All calculation methods return a new Duration to ensure the immutable properties of the Duration.

At this point, I believe you have a deeper understanding of "how to understand java.time.Duration". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Method object time two nanosecond different example content instance property part build variable learn practical deeper similar one line code interest Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Linux Docker Redmi MySQL