Java – localdate minus period gets the wrong result
•
Java
Subtract a period from localdate (such as "28 years, 1 month and 27 days") to get the wrong result
But subtracting a period (only days, such as "10282" days) gives the correct result Is there anything to note?
public static void main(String[] args) { printAgeAndBirthday(1989,2,22); printBirthdayFromPeriod(28,1,27); } private static void printBirthdayFromPeriod(int years,int months,int days) { final Period period = Period.of(years,months,days); final LocalDate Now = LocalDate.Now(); final LocalDate birthday = Now.minus(28,ChronoUnit.YEARS) .minus(1,ChronoUnit.MONTHS) .minus(27,ChronoUnit.DAYS); System.out.println("your birthday is : "+ birthday);//1989-02-19 System.out.println("your birthday is : "+ Now.minusYears(28).minusMonths(1).minusDays(27));//1989-02-19 System.out.println("your birthday is : "+ Now.minus(period));//1989-02-19 System.out.println("your birthday is : "+period.subtractFrom(Now));//1989-02-19 System.out.println("your birthday is : "+ Now.minus(Period.ofDays(10282)));//1989-02-22 } private static void printAgeAndBirthday(int year,int month,int dayOfMonth) { LocalDate today = LocalDate.Now(); LocalDate birthday = LocalDate.of(year,month,dayOfMonth); Period p = Period.between(birthday,today); long p2 = ChronoUnit.DAYS.between(birthday,today); System.out.printf("You are %d years,%d months,and %d days old. (%d days total)%n",p.getYears(),p.getMonths(),p.getDays(),p2); LocalDate nextBDay = birthday.withYear(today.getYear()); //If your birthday has occurred this year already,add 1 to the year. if (nextBDay.isBefore(today) || nextBDay.isEqual(today)) { nextBDay = nextBDay.plusYears(1); } Period p_1 = Period.between(today,nextBDay); long p_2 = ChronoUnit.DAYS.between(today,nextBDay); System.out.printf("There are %d months,and %d days until your next birthday. (%d total)%n",p_1.getMonths(),p_1.getDays(),p_2); }
Console log:
You are 28 years,1 months,and 27 days old. (10282 days total) There are 10 months,and 4 days until your next birthday. (310 total) your birthday is : 1989-02-19 your birthday is : 1989-02-19 your birthday is : 1989-02-19 your birthday is : 1989-02-19 your birthday is : 1989-02-22
Java version: jdk1.0 8.0_ forty-five
Solution
Your case can be simplified to
LocalDate date1 = LocalDate.of(2017,22),date2 = LocalDate.of(2017,4,18); Period p = Period.between(date1,date2); System.out.println("date1 + p: "+date1.plus(p)); System.out.println("date2 - p: "+date2.minus(p));
This will print
date1 + p: 2017-04-18 date2 - p: 2017-02-19
In other words, the number of years is irrelevant (unless one is a leap year and the other is not, but here neither is) The following illustrates the problem:
February March April 19 20 21 22 23 24 25 26 27 28 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ↑ │ ↑ ↑ │ └──────────────────────────── plus one Month ───────────────────────────────────────┴───────────────────────── plus 27 days ─────────────────────────────────────────┤ │ ↑ ↓ └───────────────────────── minus 27 days ────────────────────────────────────────┴─────────────────── minus one month ────────────────────────────────────────────────────────┘
If you change direction, this will change:
Period p2 = Period.between(date2,date1); System.out.println("date1 - p2: "+date1.minus(p2)); System.out.println("date2 + p2: "+date2.plus(p2));
This will print
date1 - p2: 2017-04-15 date2 + p2: 2017-02-22
Therefore, when you represent periods in years, months, and days, directions become relevant On the contrary, the average number of days between two dates is constant:
LocalDate date1 = LocalDate.of(2017,18); Period p = Period.ofDays((int)ChronoUnit.DAYS.between(date1,date2)); System.out.println("date1 + p: "+date1.plus(p)); System.out.println("date2 - p: "+date2.minus(p));
date1 + p: 2017-04-18 date2 - p: 2017-02-22
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码