详解Java中Duration类的使用方法
作者:IT利刃出鞘 发布时间:2021-07-30 20:09:28
简介
本文用示例介绍java的Duration的用法。
Duration和Period
说明
Duration类通过秒和纳秒相结合来描述一个时间量,最高精度是纳秒。时间量可以为正也可以为负,比如1天(86400秒0纳秒)、-1天(-86400秒0纳秒)、1年(31556952秒0纳秒)、1毫秒(0秒1000000纳秒)等。
Period类通过年、月、日相结合来描述一个时间量,最高精度是天。时间量可以为正也可以为负,例如2年(2年0个月0天)、3个月(0年3个月0天)、4天(0年0月4天)等。
这两个类是不可变的、线程安全的、最终类。都是JDK8新增的。
Period用法
见:详解Java中Period类的使用方法
创建方法
通过时间单位创建
基于天、时、分、秒、纳秒创建。
ofDays(), ofHours(), ofMillis(), ofMinutes(), ofNanos(), ofSeconds()。例如:
Duration fromDays = Duration.ofDays(1);
通过LocalDateTime或LocalTime
通过LocalDateTime或者LocalTime 类,然后使用between获取创建Duration。
LocalDateTime start = LocalDateTime.of(2022, 1, 1, 8, 0, 0);
LocalDateTime end = LocalDateTime.of(2022, 1, 2, 8, 30, 30);
Duration duration = Duration.between(start, end);
通过已有的Duration
Duration du1 = Duration.ofHours(10);
Duration duration = Duration.from(du1);
解析方法
用法说明
用法示例
Duration fromChar1 = Duration.parse("P1DT1H10M10.5S");
Duration fromChar2 = Duration.parse("PT10M");
格式说明
采用ISO-8601时间格式。格式为:PnYnMnDTnHnMnS (n为个数)
例如:P1Y2M10DT2H30M15.03S
P:开始标记
1Y:一年
2M:两个月
10D:十天
T:日期和时间的分割标记
2H:两个小时
30M:三十分钟
15S:15.02秒
详解
1."P", "D", "H", "M" 和 "S"可以是大写或者小写(建议大写)
2.可以用“-”表示负数
示例大全
"PT20.345S" -- parses as "20.345 seconds"
"PT15M" -- parses as "15 minutes" (where a minute is 60 seconds)
"PT10H" -- parses as "10 hours" (where an hour is 3600 seconds)
"P2D" -- parses as "2 days" (where a day is 24 hours or 86400 seconds)
"P2DT3H4M" -- parses as "2 days, 3 hours and 4 minutes"
"P-6H3M" -- parses as "-6 hours and +3 minutes"
"-P6H3M" -- parses as "-6 hours and -3 minutes"
"-P-6H+3M" -- parses as "+6 hours and -3 minutes"
源码:
public final class Duration
implements TemporalAmount, Comparable<Duration>, Serializable {
//其他代码
//-----------------------------------------------------------------------
/**
* Obtains a {@code Duration} from a text string such as {@code PnDTnHnMn.nS}.
* <p>
* This will parse a textual representation of a duration, including the
* string produced by {@code toString()}. The formats accepted are based
* on the ISO-8601 duration format {@code PnDTnHnMn.nS} with days
* considered to be exactly 24 hours.
* <p>
* The string starts with an optional sign, denoted by the ASCII negative
* or positive symbol. If negative, the whole period is negated.
* The ASCII letter "P" is next in upper or lower case.
* There are then four sections, each consisting of a number and a suffix.
* The sections have suffixes in ASCII of "D", "H", "M" and "S" for
* days, hours, minutes and seconds, accepted in upper or lower case.
* The suffixes must occur in order. The ASCII letter "T" must occur before
* the first occurrence, if any, of an hour, minute or second section.
* At least one of the four sections must be present, and if "T" is present
* there must be at least one section after the "T".
* The number part of each section must consist of one or more ASCII digits.
* The number may be prefixed by the ASCII negative or positive symbol.
* The number of days, hours and minutes must parse to an {@code long}.
* The number of seconds must parse to an {@code long} with optional fraction.
* The decimal point may be either a dot or a comma.
* The fractional part may have from zero to 9 digits.
* <p>
* The leading plus/minus sign, and negative values for other units are
* not part of the ISO-8601 standard.
* <p>
* Examples:
* <pre>
* "PT20.345S" -- parses as "20.345 seconds"
* "PT15M" -- parses as "15 minutes" (where a minute is 60 seconds)
* "PT10H" -- parses as "10 hours" (where an hour is 3600 seconds)
* "P2D" -- parses as "2 days" (where a day is 24 hours or 86400 seconds)
* "P2DT3H4M" -- parses as "2 days, 3 hours and 4 minutes"
* "P-6H3M" -- parses as "-6 hours and +3 minutes"
* "-P6H3M" -- parses as "-6 hours and -3 minutes"
* "-P-6H+3M" -- parses as "+6 hours and -3 minutes"
* </pre>
*
* @param text the text to parse, not null
* @return the parsed duration, not null
* @throws DateTimeParseException if the text cannot be parsed to a duration
*/
public static Duration parse(CharSequence text) {
......
}
}
比较方法
比较两个时间的差
Instant start = Instant.parse("2017-10-03T10:15:30.00Z");
Instant end = Instant.parse("2017-10-03T10:16:30.00Z");
// start - end
Duration duration = Duration.between(start, end);
// 任何一个时间单元为负数,则返回true。true:end早于start
duration.isNegative();
Duration.between(start, end).getSeconds();
Duration.between(start, end).getNano();
增减方法
plusX()、minusX()
X表示days, hours, millis, minutes, nanos 或 seconds
Duration duration = Duration.ofHours(2);
Duration newDuration = duration.plusSeconds(33);
plus()/minus()方法
带TemporalUnit 类型参数进行加减:
Duration duration = Duration.ofHours(2);
Duration newDuration = duration.plus(33, ChronoUnit.SECONDS);
转换单位
可以用toX来转换为其他单位,支持:toDays, toHours, toMinutes, toMillis, toNanos
Duration duration = Duration.ofHours(2);
duration.toDays(); // 0
duration.toHours(); // 2
duration.toMinutes(); // 120
duration.toMillis(); // 7200000
duration.toNanos(); // 7200000000000
取值方法
可以用getX来获得指定位置的值,因为Duration是由秒和纳秒组成,所以只能获得秒和纳秒:
Duration duration = Duration.ofHours(2);
duration.getSeconds(); //7200
duration.getNano(); //
来源:https://blog.csdn.net/feiying0canglang/article/details/124918218
猜你喜欢
- 1. 栈1.1 概念栈:是一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。特点:栈中的数据元素遵循先进后出的原则,但要注意进的
- 1.会话会话: 用户打开了一个浏览器,点击了很多超链接,访问多个web次元,关闭浏览器,这个过程可以称之为会话有状态会话: 带有访问记录的会
- JDK1.7以及以前:接口(interface)在JDK7及之前的版本对接口的要求:接口定义:使用 interface 关键字 。接口中的
- 一、demo简介1.效果展示如下图,我截了三个瞬间,但其实这是一个连续的动画,就是这个大圆不停地吞下小圆。2.这个动画可以拆分为两部分,首先
- 一、synchronized 有不足新事物的出现要不是替代老事物,要么就是对老事物的补充JUC 的 locks 就是对 synchroniz
- 十六进制字符串与数值类型之间转换(C# 编程指南) 以下示例演示如何执行下列任务: 获取字符串中每个字符的十六进制值。 获取与十六进制字符串
- //构造文件File类File f=new File(fileName);//判断是否为目录f.isDirectory();//获取目录下的
- 1. matlab的lp2lp函数的作用去归一化 H(s) 的分母2. matlab的lp2lp函数的使用方法[z, p, k]=butta
- 懒加载---就是我们在spring容器启动的是先不把所有的bean都加载到spring的容器中去,而是在当需要用的时候,才把这个对象实例化到
- spring.activemq.pool.enabled=false时,每发送一条数据都需要创建一个连接,这样会出现频繁创建和销毁连接的场景
- 用java实现循环队列的方法:1、添加一个属性size用来记录眼下的元素个数。目的是当head=rear的时候。通过size=0还是size
- 概述在移动应用开发中,消息推送可以说是一项非常重要的功能,它能够起到提醒或者唤醒用户的作用,同时也是产品运营人员更高效地实现运营目标的重要手
- jar包运行时提示jar中没有主清单属性解决办法在pom文件中添加<build> &n
- Maven中建立的依赖管理方式基本已成为Java语言依赖管理的事实标准,Maven的替代者Gradle也基本沿用了Maven的依赖管理机制。
- 方法参数public String listFireEvent(@Valid FireSearch fireSearch, Ht
- 1、找准入口,使用ClassPathXmlApplicationContext的构造方法加载配置文件,用于加载classPath下的配置文件
- @schedule 注解 是springboot 常用的定时任务注解,使用起来简单方便,但是如果定时任务非常多,或者有的任务很耗时
- 本文实例为大家分享了Java实现简单幸运抽奖的具体代码,供大家参考,具体内容如下代码模块:User类:package test1;publi
- 一、前言Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用
- 前言Hello!上一期我大致讲解了关于Collection单列集合以及它的子接口List集合的概述、特点和遍历等,今天我为大家讲解关于Col