period的核心含义
duration和period的区别?
period是啥意思?
duration和period的区别为
duration释义:
n. 持续,持续的时间,期间;音长,音延
例句:
The duration of the school year will be spent in quality-oriented education.
本学年的全部时间将用作素质教育。
词组:
for the duration在整个非常时期内
short duration短期间,短历时
long duration长持续时间
period释义:
n. 周期,期间;时期;一段时间;经期;课时;句点,句号
adj. 某一时代的
例句:
In this severe period, the government needs a high degree of cooperation from the people.
在这个严峻的时期,政府需要民众的高度配合。
duration和period的区别?
3.Period和Duration的区别
(1)包含属性不同
Period包含年数,月数,天数,而Duration只包含秒,纳秒。
Period只能返回年数,月数,天数;Duration可以返回天数,小时数,分钟数,毫秒数等。
(2)between方法可以使用的类型不同
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)
(3)between获取天数差的区别
通过上面的实例可以看出:
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。
(4)Period有获取总月数的方法,为什么没有获取总天数方法?
Period有获取总月数的方法:
public long toTotalMonths() {
return years * 12L months // no overflow
}
为什么没有获取总天数方法?
因为between后获取到的Period,不会记录2个日期中间的闰年信息,有闰年的存在,每年的天数不一定是365天,所以计算不准确。