DateDiff.java
运行结果:
The 21st century (up to 2015-10-06) is P14Y9M6D old
The 21st century is 14 years, 9 months and 6 days old
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
本文介绍了一个简单的Java程序,用于计算从20世纪末至今的确切年月日差距。通过使用Java的时间API,该程序展示了如何获取当前日期,并计算出21世纪已经过去的准确时间。
1328

被折叠的 条评论
为什么被折叠?



