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 , 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中文网其他相关文章!

新兴技术对Java的平台独立性既有威胁也有增强。1)云计算和容器化技术如Docker增强了Java的平台独立性,但需要优化以适应不同云环境。2)WebAssembly通过GraalVM编译Java代码,扩展了其平台独立性,但需与其他语言竞争性能。

不同JVM实现都能提供平台独立性,但表现略有不同。1.OracleHotSpot和OpenJDKJVM在平台独立性上表现相似,但OpenJDK可能需额外配置。2.IBMJ9JVM在特定操作系统上表现优化。3.GraalVM支持多语言,需额外配置。4.AzulZingJVM需特定平台调整。

平台独立性通过在多种操作系统上运行同一套代码,降低开发成本和缩短开发时间。具体表现为:1.减少开发时间,只需维护一套代码;2.降低维护成本,统一测试流程;3.快速迭代和团队协作,简化部署过程。

Java'splatformindependencefacilitatescodereusebyallowingbytecodetorunonanyplatformwithaJVM.1)Developerscanwritecodeonceforconsistentbehavioracrossplatforms.2)Maintenanceisreducedascodedoesn'tneedrewriting.3)Librariesandframeworkscanbesharedacrossproj

要解决Java应用程序中的平台特定问题,可以采取以下步骤:1.使用Java的System类查看系统属性以了解运行环境。2.利用File类或java.nio.file包处理文件路径。3.根据操作系统条件加载本地库。4.使用VisualVM或JProfiler优化跨平台性能。5.通过Docker容器化确保测试环境与生产环境一致。6.利用GitHubActions在多个平台上进行自动化测试。这些方法有助于有效地解决Java应用程序中的平台特定问题。

类加载器通过统一的类文件格式、动态加载、双亲委派模型和平台无关的字节码,确保Java程序在不同平台上的一致性和兼容性,实现平台独立性。

Java编译器生成的代码是平台无关的,但最终执行的代码是平台特定的。1.Java源代码编译成平台无关的字节码。2.JVM将字节码转换为特定平台的机器码,确保跨平台运行但性能可能不同。

多线程在现代编程中重要,因为它能提高程序的响应性和资源利用率,并处理复杂的并发任务。JVM通过线程映射、调度机制和同步锁机制,在不同操作系统上确保多线程的一致性和高效性。


热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

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

热门文章

热工具

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

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

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

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

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