我厌倦了使用java.time.Period
,结果与我手动计算的结果不同,为3天。 这里的奇怪的事情是,当我把时期分成两个时期,结果与我的手工计算相匹配。java.time.Period,除期给出错误结果
第二种方法就像我如何手动计算时间段。
有什么我错过了?有没有日历算法的标准方法或算法?以及java.time.Period
使用的算法是什么?
import java.time.LocalDate;
import java.time.Period;
import java.time.temporal.ChronoUnit;
public class test2 {
public static void main(String[] args) {
LocalDate d1 = LocalDate.of(2014, 2, 14);
LocalDate d2 = LocalDate.of(2017, 8, 1);
Period p = Period.between(d1, d2);
//using period between the two dates directly
System.out.println("period between " + d1.toString() + " and " + d2.toString() + " is " + p.getYears()
+ " years " + p.getMonths() + " months " + p.getDays() + " Days");
//dividing the period into two parts
p = Period.between(LocalDate.of(2014, 3, 1), d2);
System.out
.println("period between " + d1.toString() + " and " + d2.toString() + " is " + p.getYears() + " years "
+ p.getMonths() + " months " + d1.until(LocalDate.of(2014, 3, 1), ChronoUnit.DAYS) + " Days");
}
}
你究竟想要做什么?你会得到什么结果?你期望什么结果? – Dukeling