我正在阅读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());
我做错了任何帮助都非常感激。
答案 0 :(得分:7)
班级Period
代表若干年,月和日(按此顺序);如果你想获得转换它的日子。
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天。
答案 1 :(得分:3)
Period.getDays,返回句点的组成天数。
IE中。 java.time.Period是以年,月和日为单位测量的时间量。 Period.getDays返回部分天数。