我正在阅读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));
}
}
Run Code Online (Sandbox Code Playgroud)
两个输出都列在下面.
1
2467
Run Code Online (Sandbox Code Playgroud)
我认为这个结果是正确的
System.out.println(ChronoUnit.DAYS.between(clazz.sheSayYes,clazz.today));
Run Code Online (Sandbox Code Playgroud)
但是这个结果又回来了 1
System.out.println(Period.between(clazz.sheSayYes,clazz.today).getDays());
Run Code Online (Sandbox Code Playgroud)
我做错了什么任何帮助都非常感激.
该课程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));
}
Run Code Online (Sandbox Code Playgroud)
输出是
6 years, 9 months, 0 days
2466
Run Code Online (Sandbox Code Playgroud)
0天是正确的,因为今天是第26天.