1
Period period = new Period(startDate, endDate, PeriodType.yearMonthDay());

int years = period.getYears();
int months = period.getMonths();
int days = period.getDays();

if startDate = May 1, 1975 and endDate April 05, 2024
years = 48, months = 11, days = 4

but if startDate = April 30, 1975 and endDate April 05, 2024
years = 48, months = 11, days = 6

Why is the difference 2 days? expected days = 5! It just doesn’t look so logical when the user, choosing a birthday on the calendar, gets the age (from the current date) for May 1 - 4 days, and for April 30 - 6 days. This looks like an error.

joda time library is used

12
  • and is 5/04/2024 supposed to be 04/05/2024? Commented Apr 5, 2024 at 21:58
  • 2
    Can you, please, share a complete and verifiable but minimal reproducible example ? Also, consider adding the [jodatime] tag. Commented Apr 5, 2024 at 22:17
  • 2
    @user414243 no, there's nothing that jumps out, nor is it likely that someone ran into this incredibly esoteric problem before. Especially not within 24 hours or so, and I expect you'd prefer a speedy response. It's not that hard to write a self-contained example, or to do it in plain java (not android) with java.time - if jsr310 (which comes from the same guy that wrote joda-time) does not exhibit this behaviour, that is itself quite interesting information. Write a trivial java class with a main that hardcodes these timestamps of yours and prints the results. Commented Apr 5, 2024 at 22:44
  • 1
    With that code in the question, anybody can simply copy-paste this code and play with it. I'd like to take a stab at it, but your 'I do not see why I should provide a complete example' stance means that you're asking me to do that work. With you asking for free advise, that kind of entitled disrespect means even if I know I'm morally obligated to keep you in the dark. Let's not play these silly games. Retract the question or provide the requested self contained test case. Commented Apr 5, 2024 at 22:46
  • 4
    related question A Bug in java time calculating months between 2 dates - about java.time but seems to be similar problem Commented Apr 5, 2024 at 22:51

1 Answer 1

1

This seems perfectly correct to me.

In the first example, where start date is 1/5/1975 - 48 years and 11 months after the start date is 1/4/2024. From that date, is 4 more days to come to 5/4/2024. So the difference between 1/5/1975 and 5/4/2024 is 48 years, 11 months and 4 days.

In the second example, where start date is 30/4/1975 - 48 years and 11 months after the start date is 30/3/2024. From that date, it's 6 more days to come to 5/4/2024. So the difference between 30/4/1975 and 5/4/2024 is 48 years, 11 month and 6 days.

The reason why a one-day difference in the start date creates a two-day difference in the result is that the "11 months" of the result aren't all the same length. In particular, in the first example, the 11 months includes March but not April; and in the second example, the 11 months includes April but not March. Since March and April have different numbers of days, the overall calculation turns out different.

Sign up to request clarification or add additional context in comments.

1 Comment

it just doesn’t look so logical when the user, choosing a birthday on the calendar, gets the age (from the current date) for May 1 - 4 days, and for April 30 - 6 days. this looks like an error

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.