Java 持续时间
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)); } }
输出:
- 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()); } }
输出:
- 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()); } }
输出:
- 静态持续时间 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()); } }
输出:
- 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()); } }
输出:
- 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:
- 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:
- 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:
- 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:
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中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

Java 8引入了Stream API,提供了一种强大且表达力丰富的处理数据集合的方式。然而,使用Stream时,一个常见问题是:如何从forEach操作中中断或返回? 传统循环允许提前中断或返回,但Stream的forEach方法并不直接支持这种方式。本文将解释原因,并探讨在Stream处理系统中实现提前终止的替代方法。 延伸阅读: Java Stream API改进 理解Stream forEach forEach方法是一个终端操作,它对Stream中的每个元素执行一个操作。它的设计意图是处

PHP是一种广泛应用于服务器端的脚本语言,特别适合web开发。1.PHP可以嵌入HTML,处理HTTP请求和响应,支持多种数据库。2.PHP用于生成动态网页内容,处理表单数据,访问数据库等,具有强大的社区支持和开源资源。3.PHP是解释型语言,执行过程包括词法分析、语法分析、编译和执行。4.PHP可以与MySQL结合用于用户注册系统等高级应用。5.调试PHP时,可使用error_reporting()和var_dump()等函数。6.优化PHP代码可通过缓存机制、优化数据库查询和使用内置函数。7

PHP和Python各有优势,选择应基于项目需求。1.PHP适合web开发,语法简单,执行效率高。2.Python适用于数据科学和机器学习,语法简洁,库丰富。

胶囊是一种三维几何图形,由一个圆柱体和两端各一个半球体组成。胶囊的体积可以通过将圆柱体的体积和两端半球体的体积相加来计算。本教程将讨论如何使用不同的方法在Java中计算给定胶囊的体积。 胶囊体积公式 胶囊体积的公式如下: 胶囊体积 = 圆柱体体积 两个半球体体积 其中, r: 半球体的半径。 h: 圆柱体的高度(不包括半球体)。 例子 1 输入 半径 = 5 单位 高度 = 10 单位 输出 体积 = 1570.8 立方单位 解释 使用公式计算体积: 体积 = π × r2 × h (4

PHP和Python各有优势,适合不同场景。1.PHP适用于web开发,提供内置web服务器和丰富函数库。2.Python适合数据科学和机器学习,语法简洁且有强大标准库。选择时应根据项目需求决定。

PHP适合web开发,特别是在快速开发和处理动态内容方面表现出色,但不擅长数据科学和企业级应用。与Python相比,PHP在web开发中更具优势,但在数据科学领域不如Python;与Java相比,PHP在企业级应用中表现较差,但在web开发中更灵活;与JavaScript相比,PHP在后端开发中更简洁,但在前端开发中不如JavaScript。

Java是热门编程语言,适合初学者和经验丰富的开发者学习。本教程从基础概念出发,逐步深入讲解高级主题。安装Java开发工具包后,可通过创建简单的“Hello,World!”程序实践编程。理解代码后,使用命令提示符编译并运行程序,控制台上将输出“Hello,World!”。学习Java开启了编程之旅,随着掌握程度加深,可创建更复杂的应用程序。
