In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
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!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.