我是靠谱客的博主 落寞小松鼠,最近开发中收集的这篇文章主要介绍Java8新特性 - 日期相关 - 日期加减,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

[b]1. 日期相减[/b]
DateDiff.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

[b]2. 日期相加[/b]
DateAdd.java

package corejava2.date;

import java.time.LocalDate;
import java.time.Period;

/**
* DateAdd -- compute the difference between two dates (e.g., today and 700 days
* from now).
*/
public class DateAdd {
public static void main(String[] av) {
/** Today's date */
LocalDate now = LocalDate.now();

Period p = Period.ofDays(700);
LocalDate then = now.plus(p);

System.out.printf("Seven hundred days from %s is %s%n", now, then);
}
}


运行结果:
Seven hundred days from 2015-10-06 is 2017-09-05

最后

以上就是落寞小松鼠为你收集整理的Java8新特性 - 日期相关 - 日期加减的全部内容,希望文章能够帮你解决Java8新特性 - 日期相关 - 日期加减所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(75)

评论列表共有 0 条评论

立即
投稿
返回
顶部