我正在阅读有关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
表示年份,月份和天数(按此顺序);如果您只想获得转换的时间,则取决于您。
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日。