---
title: "Java8新特性 - 时间相关 - 日期相差"
author: "davidgjy"
date: "2017-10-04T13:36:57Z"
tags: []
categories: ["编程语言"]
slug: "java8"
draft: false
lastmod: "2015-10-06T16:38:44Z"
id: 2247338
url: "https://kelvingjy.iteye.com/blog/2247338"
---

DateDiff.java

```java

package corejava2.date;

import java.time.LocalDate;
import java.time.Period;

public class DateDiff {

	public static void main(String[] args) {
		/** The date at the end of the last century */
		LocalDate endofCentury = LocalDate.of(2000, 12, 31);
		LocalDate now = LocalDate.now();

		Period diff = Period.between(endofCentury, now);

		System.out.printf("The 21st century (up to %s) is %s old%n", now, diff);
		System.out.printf("The 21st century is %d years, %d months and %d days old",
				diff.getYears(), diff.getMonths(), diff.getDays());
	}
}

```


运行结果:
The 21st century (up to 2015-10-06) is P14Y9M6D old
The 21st century is 14 years, 9 months and 6 days old