final修饰,线程安全,iso-8601日历系统中基于日期的时间量,例如2年3个月4天。
主要属性:年数,月数,天数。
/** * the number of years. */ private final int years; /** * the number of months. */ private final int months; /** * the number of days. */ private final int days;
用于时间量,比较2个日期。
例如:
localdate localdate1 = localdate.of(2019, 11, 15); localdate localdate2 = localdate.of(2020, 1, 1); period p = period.between(localdate1, localdate2); system.out.println("years:"+p.getyears()+" months:"+p.getmonths()+" days:"+p.getdays());
输出:
years:0 months:1 days:17
final修饰,线程安全,基于时间的时间量,如“34.5秒”。
主要属性:秒,纳秒
/** * the number of seconds in the duration. */ private final long seconds; /** * the number of nanoseconds in the duration, expressed as a fraction of the * number of seconds. this is always positive, and never exceeds 999,999,999. */ private final int nanos;
用于时间量,比较2个时间。
例如:
localdatetime localdatetime1 = localdatetime.of(2019, 11, 15, 0, 0); localdatetime localdatetime2 = localdatetime.of(2019, 11, 15, 10, 30); duration d = duration.between(localdatetime1, localdatetime2); system.out.println("days:"+d.todays()); system.out.println("hours:"+d.tohours()); system.out.println("minutes:"+d.tominutes()); system.out.println("millis:"+d.tomillis());
输出:
days:0
hours:10
minutes:630
millis:37800000
period包含年数,月数,天数,而duration只包含秒,纳秒。
period只能返回年数,月数,天数;duration可以返回天数,小时数,分钟数,毫秒数等。
period只能使用localdate,duration可以使用所有包含了time部分且实现了temporal接口的类,比如localdatetime,localtime和instant等。
period:
public static period between(localdate startdateinclusive, localdate enddateexclusive)
duration:
public static duration between(temporal startinclusive, temporal endexclusive)
通过上面的实例可以看出:
period p.getdays() 获取天数时,只会获取days属性值,而不会将年月部分都计算成天数,不会有2020.1.1和2019.1.1比较后获取天数为365天的情况。
public int getdays() { return days; }
duration d.todays() 获取天数时,会将秒属性转换成天数。
public long todays() { return seconds / seconds_per_day; }
所以,想要获取2个时间的相差总天数,只能用duration。
period有获取总月数的方法:
public long tototalmonths() { return years * 12l + months; // no overflow }
为什么没有获取总天数方法?
因为between后获取到的period,不会记录2个日期中间的闰年信息,有闰年的存在,每年的天数不一定是365天,所以计算不准确。
如对本文有疑问, 点击进行留言回复!!
现在微服务这么火,你还不了解吗?阿里P8推荐的微服务学习指南
论文笔记:SlowFast Networks for Video Recognition
网友评论