je fatigué en utilisant java.time.Period
, et les résultats étaient différents de mes calculs manuels de trois jours. La chose étrange ici est quand je divise la période en deux périodes, les résultats correspondent à mes calculs manuels.java.time.Period, en divisant la période donne des résultats erronés
la seconde méthode est juste comme comment je calcule la période manuellement.
Y at-il quelque chose que j'ai raté? existe-t-il une méthode standard ou un algorithme d'arithmétique de calendrier? et quel est l'algorithme utilisé par 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");
}
}
Qu'essayez-vous exactement de faire? Quels résultats obtenez-vous? Quels résultats attendez-vous? – Dukeling