Duration表示的是一个较短的时间段,也就是一个持续时间,比如5.3秒,200毫秒,精确到秒或纳秒,有一个和Duration类似的类是Peroid,Peroid表示的也是一个时间段,比Duration时间更长一些,比如n年、x月、m日。
这里我们重点介绍Duration,我们把Duration的API方法的使用都列举了一遍。
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalUnit;
import java.util.List;
/**
* Duration:表示一个时间段,单位是秒或纳秒,例如20秒、10纳秒
*
* 时间单位换算:
* 1,000 皮秒 = 1纳秒
*
* 1,000,000 皮秒 = 1微秒
*
* 1,000,000,000 皮秒 = 1毫秒
*
* 1,000,000,000,000 皮秒 = 1秒
*/
public class DurationTest {
public static void main(String[] args) {
//abs 求绝对值
Duration durationOneDay = Duration.ofDays(-1);
System.out.println(durationOneDay);
System.out.println("abs:"+durationOneDay.abs());
System.out.println("-----------------------------------------");
//addTo 将当前持续时间加到一个时间对象上
LocalDateTime now = LocalDateTime.now();
System.out.println("LocalDateTime now:"+now);
Temporal start = durationOneDay.addTo(now);
System.out.println("addTo:"+start);
System.out.println("-----------------------------------------");
//between 计算两个Duration之间的时间差
Temporal end = now;
System.out.println("between:"+Duration.between(start,end));
System.out.println("-----------------------------------------");
//compareTo 比较两个Duration之间的大小,大于=1 等于=0 小于=-1
Duration duration15Hours = Duration.ofHours(15);
int i = duration15Hours.compareTo(durationOneDay);
Duration duration15Hours2 = Duration.ofHours(15);
int i1 = duration15Hours.compareTo(duration15Hours2);
Duration duration2Day = Duration.ofDays(2);
int i2 = duration15Hours.compareTo(duration2Day);
System.out.println(duration15Hours + " compareTo " + durationOneDay +" ="+i);
System.out.println(duration15Hours + " compareTo " + duration15Hours2 +" ="+i1);
System.out.println(duration15Hours + " compareTo " + duration2Day + " ="+i2);
System.out.println("-----------------------------------------");
//dividedBy 对时间段进行除法运算
Duration durationDividedBy = duration2Day.dividedBy(2);
System.out.println("dividedBy:"+durationDividedBy);
System.out.println("-----------------------------------------");
//equals
boolean equals = duration15Hours.equals(duration15Hours2);
boolean equals2 = duration15Hours == duration15Hours2;
System.out.println(duration15Hours + " equals " + duration15Hours2 +" ="+equals);
System.out.println(duration15Hours + " == " + duration15Hours2 +" ="+equals2);
/*System.out.println(duration15Hours.getSeconds());
System.out.println(duration15Hours2.getSeconds());
System.out.println(duration15Hours.getNano());
System.out.println(duration15Hours2.getNano());*/
System.out.println("-----------------------------------------");
//from
Duration durationFrom = Duration.ofHours(5);
Duration from = Duration.from(durationFrom);
System.out.println("from:"+from);
System.out.println("-----------------------------------------");
//get
long get = duration2Day.get(ChronoUnit.SECONDS);
System.out.println("get:"+get+"seconds");
System.out.println("-----------------------------------------");
//getNano
int nano = duration2Day.getNano();
System.out.println("getNano:"+nano);
System.out.println("-----------------------------------------");
//getSeconds
long seconds = duration2Day.getSeconds();
System.out.println("getSeconds:"+seconds);
System.out.println("-----------------------------------------");
//getUnits
List<TemporalUnit> units = duration2Day.getUnits();
units.forEach(System.out::println);
System.out.println("-----------------------------------------");
//isNegative
System.out.println(durationOneDay+":isNegative:"+durationOneDay.isNegative());
System.out.println(duration2Day+":isNegative:"+duration2Day.isNegative());
System.out.println("-----------------------------------------");
//isZero
System.out.println(durationOneDay+":isZero:"+durationOneDay.isZero());
System.out.println(duration2Day+":isZero:"+duration2Day.isZero());
System.out.println("-----------------------------------------");
//minus
System.out.println(duration2Day+"-"+ duration15Hours +"="+duration2Day.minus(duration15Hours));
System.out.println(duration2Day+"-1day="+duration2Day.minusDays(1));
System.out.println(duration2Day+"-1hour="+duration2Day.minusHours(2));
System.out.println("-----------------------------------------");
//multipliedBy
System.out.println(duration2Day+"*3="+duration2Day.multipliedBy(5));
System.out.println("-----------------------------------------");
//negated
System.out.println("negated:"+duration2Day.negated());
System.out.println("-----------------------------------------");
//of
Duration of = Duration.of(10, ChronoUnit.HOURS);
System.out.println("of:"+of);
System.out.println("ofHours:"+Duration.ofHours(15));
System.out.println("ofNanos:"+Duration.ofNanos(15000));
System.out.println("-----------------------------------------");
//plus
Duration plus = duration15Hours.plus(duration15Hours2);
System.out.println(duration15Hours +"plus:"+duration15Hours2+"="+plus);
Duration plusDays = duration15Hours.plusDays(5);
System.out.println(duration15Hours +"plus:"+5+"Days="+plusDays);
System.out.println("-----------------------------------------");
//subtractFrom
Temporal temporalSubtract = duration15Hours.subtractFrom(now);
System.out.println("["+now +"] subtract: ["+now+"]="+temporalSubtract);
System.out.println("-----------------------------------------");
//to
System.out.println(duration15Hours.plusDays(3).toDays());
System.out.println(duration15Hours.toDays());
System.out.println(duration15Hours.toHours());
System.out.println(duration15Hours.toMillis());
System.out.println("-----------------------------------------");
//with
System.out.println(duration15Hours.withNanos(1000));
System.out.println(duration15Hours.withSeconds(1000));
System.out.println("-----------------------------------------");
}
}
输出:
PT-24H
abs:PT24H
-----------------------------------------
LocalDateTime now:2021-07-12T15:22:51
addTo:2021-07-11T15:22:51
-----------------------------------------
between:PT24H
-----------------------------------------
PT15H compareTo PT-24H =1
PT15H compareTo PT15H =0
PT15H compareTo PT48H =-1
-----------------------------------------
dividedBy:PT24H
-----------------------------------------
PT15H equals PT15H =true
PT15H == PT15H =false
-----------------------------------------
from:PT5H
-----------------------------------------
get:172800seconds
-----------------------------------------
getNano:0
-----------------------------------------
getSeconds:172800
-----------------------------------------
Seconds
Nanos
-----------------------------------------
PT-24H:isNegative:true
PT48H:isNegative:false
-----------------------------------------
PT-24H:isZero:false
PT48H:isZero:false
-----------------------------------------
PT48H-PT15H=PT33H
PT48H-1day=PT24H
PT48H-1hour=PT46H
-----------------------------------------
PT48H*3=PT240H
-----------------------------------------
negated:PT-48H
-----------------------------------------
of:PT10H
ofHours:PT15H
ofNanos:PT0.000015S
-----------------------------------------
PT15Hplus:PT15H=PT30H
PT15Hplus:5Days=PT135H
-----------------------------------------
[2021-07-12T15:22:51] subtract: [2021-07-12T15:22:51]=2021-07-12T00:22:51
-----------------------------------------
3
0
15
54000000
-----------------------------------------
PT15H0.000001S
PT16M40S
-----------------------------------------
Process finished with exit code 0