我厭倦了使用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