20230523 java.time.Duration
介绍
- java.time.Duration
- 类声明
@jdk.internal.ValueBased
public final class Duration implements TemporalAmount, Comparable<Duration>, Serializable
-
两个时刻之间的时间量
-
两个
Instant
之间的时长是Duration
-
在内部,秒数存储在一个
long
中 (seconds
),而纳秒数存储在一个额外的int
中 (nanos
) -
Duration
是基于时间的TemporalAmount
实现,存储秒和纳秒,但使用其他基于Duration
的单位(例如分钟、小时和固定的 24 小时制日)提供一些访问Period
是一个基于日期的TemporalAmount
实现,存储年月日。
-
实现
TemporalAmount
, 可以获取的时间单位:- NANOS
- SECONDS
-
支持换算为 日、小时、分钟、秒、毫秒、纳秒
API
常量
- ZERO : PT0S
- 0s
static
-
between
Duration between(Temporal startInclusive, Temporal endExclusive)
-
from
Duration from(TemporalAmount amount)
-
of
Duration of(long amount, TemporalUnit unit)
-
ofDays
Duration ofDays(long days)
-
ofHours
-
ofMinutes
-
ofSeconds
-
ofMillis
-
ofNanos
-
parse
- 反
toString
- 反
public
-
withSeconds
- 返回具有指定秒数的该持续时间的副本
-
withNanos
- 返回具有指定纳秒数的该持续时间的副本
-
getSeconds
-
getNano
-
isNegative
- 是否负值
-
isZero
- 是否 0
-
truncatedTo
- 截断
-
toString
- 返回持续时间的 ISO-8601 表示形式
- 格式:
PTnHnMnS
- 例如
PT8H6M12.345S
表示8小时6分12.345秒
算数计算
-
minus
- 减
-
minusDays
-
minusHours
-
minusMillis
-
minusMinutes
-
minusNanos
-
minusSeconds
-
plus
- 加
-
plusDays
-
plusHours
-
plusMillis
-
plusMinutes
-
plusNanos
-
plusSeconds
-
negated
- 取负值
-
abs
- 取绝对值
-
multipliedBy
- 乘
-
dividedBy
- 除
根据时间单位获取值
- toDays
- toDaysPart
- toHours
- toHoursPart
- toMinutes
- 换算为分钟单位
- 例如 PT1H2M3S (1小时2分钟3秒钟) ,结果为 62
- toMinutesPart
- 得到分钟的部分值
- 例如 PT1H2M3S (1小时2分钟3秒钟) ,结果为 2
- toSeconds
- toSecondsPart
- toMillis
- toMillisPart
- toNanos
- toNanosPart
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
2019-09-05 20190905 Lombok常用注解
2017-09-05 程序员你为什么这么累?