LocalDate.plus 不正确的答案
2022-09-02 20:59:44
Java的LocalDate API似乎在调用长时给出了不正确的答案,其中我得到了一个错误。我在这里做错了什么吗?plus(...)
Period
import java.time.LocalDate;
import java.time.Month;
import java.time.Period;
import java.time.temporal.ChronoUnit;
public class Main
{
public static void main(String[] args)
{
// Long Period
LocalDate birthA = LocalDate.of(1965, Month.SEPTEMBER, 27);
LocalDate eventA = LocalDate.of(1992, Month.MAY, 9);
LocalDate halfA = eventA.plus(Period.between(birthA, eventA));
System.out.println(halfA); // 2018-12-21 ????
System.out.println(ChronoUnit.DAYS.between(birthA, eventA)); // 9721
System.out.println(ChronoUnit.DAYS.between(eventA, halfA)); // 9722 ????
// Short Period
LocalDate birthB = LocalDate.of(2012, Month.SEPTEMBER, 10);
LocalDate eventB = LocalDate.of(2012, Month.SEPTEMBER, 12);
LocalDate halfB = eventB.plus(Period.between(birthB, eventB));
System.out.println(halfB); // 2018-09-14
System.out.println(ChronoUnit.DAYS.between(birthB, eventB)); // 2
System.out.println(ChronoUnit.DAYS.between(eventB, halfB)); // 2
}
}