首页 > Java > java教程 > Java 持续时间

Java 持续时间

王林
发布: 2024-08-30 15:52:12
原创
1022 人浏览过

java中的Duration是一个用于测量以秒和纳秒为单位的时间的类。 java中duration类的包是java.time.Duration。 Duration 类对象指定时间段或用于确定两个时间之间的差异。 Duration 对象是不可变的并且是线程安全的,因为 Duration 对象是不可变的,因此一旦创建它,​​我们就无法更改它的值。但是,我们可以基于另一个 Duration 对象创建新的 Duration 对象。 Duration 类继承了一个对象类(因为对象是 java 中所有类的超类)并实现了 Comparable 接口。

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

语法

java中Duration类声明的语法是:

public final class Duration extends Object implements Comparable < Duration >,
TemporalAmount, Serializable
{
//
variables and method of the class Duration}
登录后复制

持续时间列表

Duration 类方法列表在下面通过示例代码进行了解释;示例代码可以进一步用于类似的方法(对于未给出的每个方法示例代码):

  • Duration abs():此方法返回此持续时间的副本,其长度为正。
  • long get(TemporalUnit unit):返回请求单位的值。
  • staticuration Between(Temporal startInclusive, Temporal endExclusive):返回duration对象,即两个时间对象之间的持续时间。

示例#1

我们通过下面的示例java代码来理解上述方法。

代码:

package p1;
import java.time.Duration;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
public class DurationClassDemo {
public static void main(String[] args) {
Duration d = Duration.between(LocalTime.MAX,LocalTime.MIN);
System.out.println(d.get(ChronoUnit.SECONDS));
Duration absd = d.abs();
System.out.println(absd.get(ChronoUnit.SECONDS));
}
}
登录后复制

输出:

Java 持续时间

  • Temporal addTo(Temporaltemporal):返回duration对象,即temporal与thisduration对象相加。
  • Duration splitBy(long divisor):返回duration对象,即这个duration除以除数。

示例#2

我们通过下面的示例java代码来理解上述方法:

代码:

package p1;
import java.time.Duration;
import java.time.*;
import java.time.temporal.ChronoUnit;
public class DurationClassDemo {
public static void main(String[] args) {
Duration d = Duration.between(LocalTime.MAX,LocalTime.MIN);
LocalDateTime date = LocalDateTime.now();
System.out.println(date);
date = (LocalDateTime)d.addTo(date);
System.out.println(date);
Duration d1 = d.dividedBy(4);
System.out.println(d1);
System.out.println(d.getSeconds());
System.out.println(d1.getSeconds());
}
}
登录后复制

输出:

Java 持续时间

  • intcompareTo(Duration otherDuration):此时长与指定时长进行比较。
  • boolean equals(Object otherDuration):检查指定 Duration 的持续时间并返回布尔值。
  • boolean isNegative():如果此持续时间为负数,则返回 True。
  • boolean isZero():如果此持续时间长度为零,则返回 True。

示例#3

我们通过下面的示例java代码来理解上述方法:

代码:

package p1;
import java.time.Duration;
import java.time.*;
import java.time.temporal.ChronoUnit;
public class DurationClassDemo {
public static void main(String[] args) {
Duration d = Duration.between(LocalTime.NOON,LocalTime.MAX);
Duration d1 = Duration.between(LocalTime.NOON,LocalTime.MIN);
System.out.println(d1.getSeconds());
System.out.println(d.compareTo(d1));
System.out.println(d1.compareTo(d1));
System.out.println(d1.compareTo(d));
System.out.println(d1.equals(d));
System.out.println(d1.isNegative());
System.out.println(d1.isZero());
}
}
登录后复制

输出:

Java 持续时间

  • 静态持续时间 from(TemporalAmount amount):从时间量获取持续时间实例。

示例#4

我们通过下面的示例java代码来理解上述方法:

代码:

package p1;
import java.time.Duration;
import java.time.*;
import java.time.temporal.ChronoUnit;
public class DurationClassDemo {
public static void main(String[] args) {
Duration d = Duration.from(ChronoUnit.DAYS.getDuration());
System.out.println(d.toMinutes());
}
}
登录后复制

输出:

Java 持续时间

  • int getNano():返回以纳秒为单位的持续时间。
  • long getSeconds():返回以秒为单位的持续时间。
  • 列表 getUnits(): 返回此持续时间支持的单位集。
  • int hashCode():返回此持续时间的哈希代码。

示例#5

我们通过下面的示例java代码来理解上述方法:

代码:

package p1;
import java.time.Duration;
import java.time.*;
import java.time.temporal.ChronoUnit;
public class DurationClassDemo {
public static void main(String[] args) {
Duration d = Duration.between(LocalTime.NOON,LocalTime.MAX);
System.out.println(d.getUnits());
System.out.println(d.toMinutes());
System.out.println(d.getSeconds());
System.out.println(d.getNano());
System.out.println(d.getClass());
}
}
登录后复制

输出:

Java 持续时间

  • Duration minus(Duration duration):- Returns object which results from this duration subtracted with the specified duration.
  • Duration minus(long amountToSubtract, TemporalUnit unit): Returns object resulting from this duration subtracted with the specified duration.
  • Duration minusDays(long daysToSubtract): Returns object which results from this duration subtracted with the specified duration in standard 24-hour days.
  • Duration minusHours(long hoursToSubtract): Returns object resulting from this duration subtracted with the specified duration in hours.
  • Duration minusMillis(long millisToSubtract): Returns object resulting from this duration subtracted with the specified duration in milliseconds.
  • Duration minusMinutes(long minutesToSubtract): Returns object resulting from this duration subtracted with the specified duration in minutes.
  • Duration minusNanos(long nanosToSubtract): Returns object resulting from this duration subtracted with the specified duration in nanoseconds.
  • Duration minusSeconds(long secondsToSubtract): Returns object resulting from this duration subtracted with the specified duration in seconds.
  • Duration multipliedBy(long multiplicand): Returns object resulting from this duration multiplied by the scalar.
  • Duration negated() – Returns object which results from this duration with the length negated.
  • static duration of(long amount, TemporalUnit unit): Returns Duration object representing an amount in the specified unit.
  • static Duration ofDays(long days): Returns Duration object of standard 24-hour days.
  • static Duration ofHours(long hours): Returns Duration object of the hour.
  • static Duration ofMillis(long millis): Returns Duration object of milliseconds.
  • static Duration ofMinutes(long minutes): Returns Duration object of minutes.
  • static Duration ofNanos(long nanos): Returns Duration object of nanoseconds.
  • static Duration ofSeconds(long seconds): Returns Duration object of seconds.
  • static Duration ofSeconds(long seconds, long nanoAdjustment): Returns Duration object of seconds and nanoseconds adjustment.

Example #6

We understand the above methods with the below sample java code:

Code:

package p1;
import java.time.Duration;
import java.time.*;
import java.time.temporal.ChronoUnit;
public class DurationClassDemo {
public static void main(String[] args) {
Duration d = Duration.ofDays(6);
System.out.println(d.getSeconds());
Duration d1 = d.minusDays(3);
System.out.println(d1.getSeconds());
d = Duration.ofHours(6);
System.out.println(d.getSeconds());
d1 = d.minusHours(2);
System.out.println(d1.getSeconds());
}
}
登录后复制

Output:

Java 持续时间

  • static Duration parse(CharSequence text): Return duration object from a text, for example, PnDTnHnMn.nS.
  • Duration plus(Duration duration): Return the duration object of this duration with added the specified duration.
  • Duration plus(long amountToAdd, TemporalUnit unit): Return the duration object of this duration with add the specified duration.
  • Duration plusDays(long daysToAdd): Return the duration object of this duration with add the specified duration in 24-hour days.
  • Duration plusHours(long hoursToAdd): Return the duration object of this duration with add the specified duration in hours.
  • Duration plusMillis(long millisToAdd): Return the duration object of this duration with add the specified duration in milliseconds.
  • Duration plusMinutes(long minutesToAdd): Return the duration object of this duration with the add specified duration in minutes.
  • Duration plusNanos(long nanosToAdd): Return the duration object of this duration with add the specified duration in nanoseconds.
  • Duration plusSeconds(long secondsToAdd): Return the duration object of this duration with the specified duration in seconds.

Example #7

We understand the above methods with the below sample java code:

Code:

package p1;
import java.time.Duration;
import java.time.*;
import java.time.temporal.ChronoUnit;
public class DurationClassDemo {
public static void main(String[] args) {
Duration d = Duration.ofDays(6);
System.out.println(d.getSeconds());
Duration d1 = d.plusDays(2);
System.out.println(d1.getSeconds());
d = Duration.ofHours(6);
System.out.println(d.getSeconds());
d1 = d.plusHours(2);
System.out.println(d1.getSeconds());
}
}
登录后复制

Output:

Java 持续时间

  • Temporal subtractFrom(Temporal temporal): Return Subtraction of this duration from the temporal object.
  • long toDays(): Return the number of days in this duration.
  • long toHours(): Return the number of hours in this duration.
  • long toMillis(): Return the number of milliseconds in this duration.
  • long toMinutes(): return the number of minutes in this duration.
  • long toNanos(): return the number of nanoseconds in this duration.
  • String toString(): Return this duration in string representation, such as PT8H6M12.345S.

Example #8

We understand the above methods with the below sample java code:

Code:

package p1;
import java.time.Duration;
import java.time.*;
import java.time.temporal.ChronoUnit;
public class DurationClassDemo {
public static void main(String[] args) {
Duration d = Duration.ofDays(6);
System.out.println(d.toHours());
Duration d1 =Duration.ofHours(24) ;
System.out.println(d1.toDays());
}
}
登录后复制

Output:

Java 持续时间

  • Duration withNanos(int nanoOfSecond): Returns duration object with the specified nanoofsecond.
  • Duration withSeconds(long seconds): Returns duration object of this duration with the seconds of the specified amount.

Example #9

We understand the above methods with the below sample java code:

Code:

package p1;
import java.time.Duration;
import java.time.*;
import java.time.temporal.ChronoUnit;
public class DurationClassDemo {
public static void main(String[] args) {
Duration d = Duration.ofDays(6);
System.out.println(d.toString());
d = d.withSeconds(3000);
System.out.println(d.toString());
}
}
登录后复制

Output:

Java 持续时间

Conclusion

The Duration class is one of the built-in class in java, which is used to measure time in seconds and nanoseconds and add, subtract, and convert the duration, or, in simple words, the duration class allows performance operation on time or day duration. The duration class is available in java.time.Duration package of java.

以上是Java 持续时间的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板