LocalDate startDate = LocalDate.of(2015, 2, 20);LocalDate endDate = LocalDate.of(2017, 1, 15);Period period = Period.between(startDate, endDate);当我们获得 Period 对象后,我们可以用 Period 对象中的 *getYears(), getMonths(), getDays() 方法来获得具体的值。logger.info(String.format("...
Duration 类 和Period 相对,Duration 类是对时间进行操作的。 具体操作的单位为秒(seconds )和纳秒(nanoseconds )。 因可以直接对纳秒进行操作,所以 Duration 能比较精确的对时间进行计算。 另外,要获得 Duration 对象,我们需要从 instants 来进行比较,使用 between() 方法来比较 instants 。 代码语言:javascript 代码...
log.info("yyyy-MM-dd = {}",(endDate.getYear()+"-"+endDate.getMonth().getValue()+"-"+endDate.getDayOfMonth()));PeriodperiodZero=Period.ZERO;PeriodperiodYMD=Period.of(20,3,1);PeriodperiodYear=Period.ofYears(1);PeriodperiodMonth=Period.ofMonths(2);PeriodperiodDay=Period.ofDays(2);...
另外一个可以创建Period对象的方法就是使用数字,Period 中提供了一个 of 方法,我们可以用这个方法来构造一个 Period 对象: AI检测代码解析 Period fromUnits = Period.of(3, 10, 10); Period fromDays = Period.ofDays(50); Period fromMonths = Period.ofMonths(5); Period fromYears = Period.ofYears(10...
time.Duration; import java.time.Instant; public class Test { public static void main(String[] args) { Instant inst1 = Instant.now(); System.out.println("Inst1 : " + inst1); Instant inst2 = inst1.plus(Duration.ofSeconds(10)); System.out.println("Inst2 : " + inst2); System.out...
Period 对应使用 LocalDate ,它们的作用范围域都是日期(年/月/日) Duration 对应使用 Instant,它们的作用范围域都是时间(天/时/分/秒/毫秒/纳秒) LocalDate 精度到日期记录固定时间值的LocalDate,创建方式: 复制代码 LocalDatelocalDate1=LocalDate.of(2019,9,1);LocalDatelocalDate2=LocalDate.ofYearDay(2019...
Period:没有提供计算两个日期间总天数的方法,且其计算天数时不考虑闰年的影响。Duration:可以精确计算两个时间点之间的总天数。总结:Period和Duration在Java 8的日期时间API中各有其适用场景。Period主要用于表示和处理基于年、月、天的日期差异,而Duration则更为灵活,适用于表示和处理基于时间的量,...
在Java 8的日期时间API中,Period和Duration是两个重要的时间量概念。Period是线程安全的,它基于ISO-8601日历系统,用于表示如“2年3个月4天”的时间量,主要关注年、月、天的属性。它用于比较两个日期,例如,years:0 months:1 days:17。而Duration同样为final且线程安全,主要用于基于时间的量,如...
Duration类通过年月日时分秒相结合来描述一个时间量,最高精度是纳秒。时间量可以为正也可以为负,比如1天(86400秒0纳秒)、-1天(-86400秒0纳秒)、1年(31556952秒0纳秒)、1毫秒(0秒1000000纳秒)等。 Period类通过年月日相结合来描述一个时间量,最高精度是天。时间量可以为正也可以为负,例如2年(2年0个月0...
3.Period和Duration的区别 (1)包含属性不同 Period包含年数,月数,天数,而Duration只包含秒,纳秒。 Period只能返回年数,月数,天数;Duration可以返回天数,小时数,分钟数,毫秒数等。 (2)between方法可以使用的类型不同 Period只能使用LocalDate,Duration可以使用所有包含了time部分且实现了Temporal接口的类,比如LocalDateTi...