Java的.time.Duration类在Java中
Duration 是Java时间库中存在的基于值的类。它用于获取基于时间的时间量。这个类是不可变的和线程安全的。本文介绍了此类中存在的所有方法以及使用类方法的一些基本示例。
此类实现了 Serializable、Comparable
类中存在的字段:Field Description static Duration It is a constant for zero duration.
类中存在的方法:Method Description abs() This method returns positive copy of length addTo(Temporal temporal) This method adds this duration to the specified temporal object. Duration between(Temporal startInclusive, Temporal endExclusive) This method finds the duration between two Temporal objects. compareTo(Duration otherDuration) This method compares this duration to the specified Duration. dividedBy(long divisor) This method returns a copy of this duration divided by the specified value. equals(Object otherDuration) This method checks if this duration is equal to the specified Duration. from(TemporalAmount amount) This method obtains an instance of Duration from a temporal amount. get(TemporalUnit unit) This method gets the value of the requested unit. getNano() This method gets the number of nanoseconds within the second in this duration. getSeconds() This method gets the number of seconds in this duration. getUnits() This method gets the set of units supported by this duration. hashCode() A hash code for this duration. isNegative() This method checks if this duration is negative, excluding zero. isZero() This method checks if this duration is zero length. minus(Duration duration) This method returns a copy of this duration with the specified duration subtracted. minus(long amountToSubtract, TemporalUnit unit) This method returns a copy of this duration with the specified duration subtracted. minusDays(long daysToSubtract) This method returns a copy of this duration with the specified duration in standard 24-hour days subtracted. minusHours(long hoursToSubtract) This method returns a copy of this duration with the specified duration in hours subtracted. minusMillis(long millisToSubtract) This method returns a copy of this duration with the specified duration in milliseconds subtracted. minusMinutes(long minutesToSubtract) This method returns a copy of this duration with the specified duration in minutes subtracted. minusNanos(long nanosToSubtract) This method returns a copy of this duration with the specified duration in nanoseconds subtracted. minusSeconds(long secondsToSubtract) This method returns a copy of this duration with the specified duration in seconds subtracted. multipliedBy(long multiplicand) This method returns a copy of this duration multiplied by the scalar. negated() This method returns a copy of this duration with the length negated. of(long amount, TemporalUnit unit) This method obtains a Duration representing an amount in the specified unit. ofDays(long days) This method obtains a Duration representing a number of standard 24 hour days. ofHours(long hours) This method obtains a Duration representing a number of standard hours. ofMillis(long millis) This method obtains a Duration representing a number of milliseconds. ofMinutes(long minutes) This method obtains a Duration representing a number of standard minutes. ofNanos(long nanos) This method obtains a Duration representing a number of nanoseconds. ofSeconds(long seconds) This method obtains a Duration representing a number of seconds. ofSeconds(long seconds, long nanoAdjustment) This method obtains a Duration representing a number of seconds and an adjustment in nanoseconds. parse(CharSequence text) This method obtains a Duration from a text string such as PnDTnHnMn.nS. plus(Duration duration) This method returns a copy of this duration with the specified duration added. plus(long amountToAdd, TemporalUnit unit) This method returns a copy of this duration with the specified duration added. plusDays(long daysToAdd) This method returns a copy of this duration with the specified duration in standard 24 hour days added. plusHours(long hoursToAdd) This method returns a copy of this duration with the specified duration in hours added. plusMillis(long millisToAdd) This method returns a copy of this duration with the specified duration in milliseconds added. plusMinutes(long minutesToAdd) This method returns a copy of this duration with the specified duration in minutes added. plusNanos(long nanosToAdd) This method returns a copy of this duration with the specified duration in nanoseconds added. plusSeconds(long secondsToAdd) This method returns a copy of this duration with the specified duration in seconds added. subtractFrom(Temporal temporal) This method subtracts this duration from the specified temporal object. toDays() This method gets the number of days in this duration. toHours() This method gets the number of hours in this duration. toMillis() This method converts this duration to the total length in milliseconds. toMinutes() This method gets the number of minutes in this duration. toNanos() This method converts this duration to the total length in nanoseconds expressed as a long. toString() A string representation of this duration using ISO-8601 seconds based representation, such as PT8H6M12.345S. withNanos(int nanoOfSecond) This method returns a copy of this duration with the specified nano-of-second. withSeconds(long seconds) This method returns a copy of this duration with the specified amount of seconds.
示例 1:此示例说明了 Duration 类的简单使用。
Java
// Importing required classes
import java.time.Duration;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
public class GFG {
public static void main(String[] args) {
// Initializing Duration variable
Duration duration = Duration.between(LocalTime.NOON,LocalTime.MIDNIGHT);
// Printing difference between time in seconds
System.out.println(duration.get(ChronoUnit.SECONDS));
// Finiding absolute difference
Duration absDuration = duration.abs();
// Printing absolute time difference in seconds
System.out.println(absDuration.get(ChronoUnit.SECONDS));
}
}
Java
// Importing required classes
import java.time.Duration;
import java.time.temporal.ChronoUnit;
public class GFG {
public static void main(String[] args) {
// Getting duration in an hour
Duration duration = Duration.from(ChronoUnit.HOURS.getDuration());
// Printing duration in minutes
System.out.println(duration.toMinutes());
}
}
-43200
43200
示例 2:
Java
// Importing required classes
import java.time.Duration;
import java.time.temporal.ChronoUnit;
public class GFG {
public static void main(String[] args) {
// Getting duration in an hour
Duration duration = Duration.from(ChronoUnit.HOURS.getDuration());
// Printing duration in minutes
System.out.println(duration.toMinutes());
}
}
输出:
60