Quick revision
The short answer
It prints 2024-02-29, then 2024-03-29, then 2024-03-31. January 31 plus one month would be February 31, which does not exist, so Java clamps it to the last valid day, February 29 in the leap year 2024; adding another month starts from February 29 and gives March 29, while adding two months at once starts from January 31 and reaches March 31. Month arithmetic is therefore not associative, and billing code has to define its rule explicitly.
What to remember
In this answer, we saw that plusMonths keeps the day of the month and clamps it to the last valid day when the target month is shorter, so 2024-01-31 plus one month is 2024-02-29. Because clamping loses the original day, adding one month twice gives 2024-03-29 while adding two months at once gives 2024-03-31: month arithmetic is not associative. For billing, we compute every date from the stored anchor with plusMonths(n) and document the rule, so the customer anchored on the 31st returns to the 31st whenever the month allows it.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
It prints 2024-02-29, then 2024-03-29, then 2024-03-31. January 31 plus one month would be February 31, which does not exist, so Java clamps it to the last valid day, February 29 in the leap year 2024; adding another month starts from February 29 and gives March 29, while adding two months at once starts from January 31 and reaches March 31. Month arithmetic is therefore not associative, and billing code has to define its rule explicitly.
Why this question is asked
Month arithmetic looks like the simplest thing java.time does, and it is where many billing and subscription bugs hide. A customer who signs up on the 31st, a contract that renews monthly, a report for 'one month ago': each depends on what 'plus one month' means when the target month is shorter. Interviewers use this three-line snippet because the third line surprises people who got the first two right. In this answer, we will recap how plusMonths works, walk through each line, see why the last two differ, and then design a billing rule anchored on the 31st.
1A Quick Recap of plusMonths
LocalDate is a calendar date with no time and no zone. plusMonths(n) adds n to the month field, carrying into the year if needed, and keeps the day of the month. If that day does not exist in the target month, for example the 31st in a 30-day month, Java does not throw and does not roll over into the next month. It clamps the day to the last valid day of the target month. The documentation calls this adjusting to the last valid day.
2Walking Through the Three Lines
Now the snippet. 2024 is a leap year, so February has 29 days.
| Path | Step 1 | Step 2 | Result |
|---|---|---|---|
plusMonths(1).plusMonths(1) | Feb 31 -> clamped to 2024-02-29 | Feb 29 + 1 month = 2024-03-29 | 2024-03-29 |
plusMonths(2) | Mar 31 exists, no clamping | none | 2024-03-31 |
For the first line, Java computes February 31, which does not exist, and clamps it to February 29. In a non-leap year like 2023, the same call gives 2023-02-28.
For the second line, the first plusMonths(1) has already returned February 29. A LocalDate holds no memory of where it came from; the original 31 is gone. So the second plusMonths(1) starts from February 29 and gives March 29, a perfectly valid date that needs no clamping.
For the third line, plusMonths(2) starts from January 31, goes straight to March 31, which exists, and returns it. Clamping never happens, because Java never visits February.
3Why Month Arithmetic Is Not Associative
In ordinary arithmetic, adding 1 and then 1 is the same as adding 2. That property is called associativity, and it is what most developers silently assume. Month arithmetic breaks it because clamping is a lossy step: once January 31 becomes February 29, the information 'this was the 31st' is thrown away, and later steps cannot recover it.
So the same intent, 'two months after January 31', gives two answers depending on how the code is written. In a billing system that adds one month to the previous billing date in a loop, a customer anchored on the 31st drifts to the 29th after February and stays there for the rest of their life. It is not a Java bug; it is a business rule that nobody wrote down.
4Designing a Billing Rule Anchored on the 31st
The fix is to decide the rule and then compute every date from the anchor, never from the previous result. The most common rule is: bill on the anchor day, or on the last day of the month if the month is shorter. Computing from the anchor date with plusMonths(n) gives exactly that, because each call clamps independently from the original 31st.
- Load anchor date2024-01-31, stored once at sign-up
- Compute anchor.plusMonths(n)never
lastBill.plusMonths(1) - Day exists in target month?YesBill on the anchor dayNoClamp: bill on last day of month
- Test with 29th, 30th, 31st anchorsacross leap and non-leap Februaries
4 steps in order. Play them, or step through them one at a time.
Notice that the customer returns to the 31st whenever the month allows it. Another option is to store the anchor day of month, 31, and compute YearMonth.of(year, month).atDay(Math.min(anchorDay, yearMonth.lengthOfMonth())), which says the rule even more explicitly and is easy to test. Some businesses choose a different rule, such as moving bills on the 29th to 31st to the 1st of the next month or to a fixed day like the 28th; that is a product decision, but the code should make it visible rather than letting clamping decide.
5Follow-up questions
How would you implement monthly billing anchored on the 31st?
Store the anchor date, or the anchor day of the month, when the subscription starts, and compute the nth billing date as anchor.plusMonths(n), never from the previous billing date. That bills on the 31st when the month has one and on the last day otherwise: January 31, February 29, March 31, April 30. The rule should be written in the product documentation and tested with anchors on the 29th, 30th and 31st in leap and non-leap years.
Does minusMonths clamp the same way?
Yes. LocalDate.of(2024, 3, 31).minusMonths(1) gives 2024-02-29, because February 31 does not exist. The same loss of information applies, so going back and forward again does not return to the original date.
What about plusYears on February 29?
LocalDate.of(2024, 2, 29).plusYears(1) gives 2025-02-28, again clamped to the last valid day. Anniversaries and annual renewals anchored on 29 February need the same explicit rule.
6Conclusion
In this answer, we saw that plusMonths keeps the day of the month and clamps it to the last valid day when the target month is shorter, so 2024-01-31 plus one month is 2024-02-29. Because clamping loses the original day, adding one month twice gives 2024-03-29 while adding two months at once gives 2024-03-31: month arithmetic is not associative. For billing, we compute every date from the stored anchor with plusMonths(n) and document the rule, so the customer anchored on the 31st returns to the 31st whenever the month allows it.
Open with
It prints 2024-02-29, 2024-03-29 and 2024-03-31. plusMonths keeps the day of month, and if that day doesn't exist in the target month it clamps to the last valid day.
Then, if they want more
So January 31 plus one month would be February 31, which doesn't exist, and 2024 is a leap year, so we get February 29. The second line starts from that result. The LocalDate doesn't remember it came from the 31st, so February 29 plus a month is March 29.
The third line adds two months directly to January 31, which lands on March 31, a valid date, so no clamping happens. The lesson is that month arithmetic isn't associative, because clamping throws information away.
In billing, if I add one month to the previous bill date in a loop, a customer who signed up on the 31st drifts to the 29th forever. So I'd store the anchor date and compute each billing date as anchor plus n months, which gives the 31st when it exists and the last day of the month otherwise.
And I'd write that rule down with the product owner, because some businesses prefer another rule, and test anchors on the 29th, 30th and 31st across leap and non-leap years.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What is printed?": https://scrivio-interview-library.netlify.app/java/date-and-time/c8d7637a05/