Java 8 减去日期 return 1 天
Java 8 Substracting Dates return 1 day
我正在阅读 Java 8
中的新 java.time API
像这样
public final class DaysWithBaby
{
private final LocalDate sheSayYes = LocalDate.of(2008,Month.APRIL,26);
private final LocalDate today = LocalDate.now().plusDays(1);
public static void main(String[] args)
{
final DaysWithBaby clazz = new DaysWithBaby();
System.out.println(Period.between(clazz.sheSayYes,clazz.today).getDays());
System.out.println(ChronoUnit.DAYS.between(clazz.sheSayYes,clazz.today));
}
}
下面列出了两个输出。
1
2467
我认为这个结果是正确的
System.out.println(ChronoUnit.DAYS.between(clazz.sheSayYes,clazz.today));
但是这个返回的结果呢1
System.out.println(Period.between(clazz.sheSayYes,clazz.today).getDays());
我做错了什么,非常感谢任何帮助。
Period.getDays、returns 期间的天数部分。
即。 java.time.Period 是以年、月、日衡量的时间量。 Period.getDays returns 这部分的天数。
classPeriod
代表若干年月日(按此顺序);如果你只想获得转换它的天数。
private static final LocalDate sheSayYes = LocalDate.of(2008, Month.APRIL,
26);
private static final LocalDate today = LocalDate.now();
public static void main(String[] args) {
Period p = Period.between(sheSayYes, today);
System.out.printf("%d years, %d months, %d days%n", p.getYears(),
p.getMonths(), p.getDays());
System.out.println(ChronoUnit.DAYS.between(sheSayYes, today));
}
输出是
6 years, 9 months, 0 days
2466
0 天是正确的,因为今天是 26 号。
我正在阅读 Java 8
中的新 java.time API像这样
public final class DaysWithBaby
{
private final LocalDate sheSayYes = LocalDate.of(2008,Month.APRIL,26);
private final LocalDate today = LocalDate.now().plusDays(1);
public static void main(String[] args)
{
final DaysWithBaby clazz = new DaysWithBaby();
System.out.println(Period.between(clazz.sheSayYes,clazz.today).getDays());
System.out.println(ChronoUnit.DAYS.between(clazz.sheSayYes,clazz.today));
}
}
下面列出了两个输出。
1
2467
我认为这个结果是正确的
System.out.println(ChronoUnit.DAYS.between(clazz.sheSayYes,clazz.today));
但是这个返回的结果呢1
System.out.println(Period.between(clazz.sheSayYes,clazz.today).getDays());
我做错了什么,非常感谢任何帮助。
Period.getDays、returns 期间的天数部分。
即。 java.time.Period 是以年、月、日衡量的时间量。 Period.getDays returns 这部分的天数。
classPeriod
代表若干年月日(按此顺序);如果你只想获得转换它的天数。
private static final LocalDate sheSayYes = LocalDate.of(2008, Month.APRIL,
26);
private static final LocalDate today = LocalDate.now();
public static void main(String[] args) {
Period p = Period.between(sheSayYes, today);
System.out.printf("%d years, %d months, %d days%n", p.getYears(),
p.getMonths(), p.getDays());
System.out.println(ChronoUnit.DAYS.between(sheSayYes, today));
}
输出是
6 years, 9 months, 0 days
2466
0 天是正确的,因为今天是 26 号。