Quick revision
The short answer
With java.time we get the last day of the month with date.with(TemporalAdjusters.lastDayOfMonth()), the next Monday with date.with(TemporalAdjusters.next(DayOfWeek.MONDAY)), and the days between two dates with ChronoUnit.DAYS.between(start, end). For 30 business days we step forward one day at a time and count only weekdays, or wrap that loop in a custom TemporalAdjuster. Every java.time value is immutable, so each call returns a new date that we must assign.
What to remember
In this answer, we saw that java.time gives a ready-made adjuster for the last day of the month and for the next Monday, where next skips the given date and nextOrSame keeps it. We counted days with ChronoUnit.DAYS.between, which excludes the end date, and saw that Period.between answers a different question. We wrote a business-day loop that moves forward and counts only weekdays, wrapped it as a TemporalAdjuster, and kept in mind that every java.time value is immutable, so each result must be assigned.
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.
With java.time we get the last day of the month with date.with(TemporalAdjusters.lastDayOfMonth()), the next Monday with date.with(TemporalAdjusters.next(DayOfWeek.MONDAY)), and the days between two dates with ChronoUnit.DAYS.between(start, end). For 30 business days we step forward one day at a time and count only weekdays, or wrap that loop in a custom TemporalAdjuster. Every java.time value is immutable, so each call returns a new date that we must assign.
Why this question is asked
Date arithmetic shows up in almost every business system: invoice due dates, trial periods, reminders, report ranges. It is also full of small traps, such as end dates that are counted twice, months of different lengths, and code that calls a method and throws its result away. Interviewers ask this coding question because four short tasks reveal whether we know the java.time API or would hand-roll calendar maths with bugs. In this answer, we will recap the types we need, solve each of the four tasks with code and its results, look at the immutability trap that breaks many first attempts, and finish with the business-day loop and a reusable adjuster.
1A Quick Recap of LocalDate and Adjusters
LocalDate is a date without a time and without a time zone, such as 2026-11-04. It is the right type when we care about calendar days, as we do in all four tasks. We get today's date with LocalDate.now(), which uses the system time zone, or LocalDate.now(clock) when we want to control the clock in tests.
A TemporalAdjuster is a small strategy object that takes a date and returns an adjusted date. We apply one with date.with(adjuster). The JDK ships ready-made adjusters in the TemporalAdjusters class, such as firstDayOfMonth(), lastDayOfMonth(), next(dayOfWeek) and nextOrSame(dayOfWeek). Because the adjuster is an interface with one method, we can also write our own as a lambda.
ChronoUnit is an enum of units such as DAYS, WEEKS and MONTHS. Each unit has a between(a, b) method that counts how many whole units lie between two temporal values.
2Task 1 and 2: Last Day of the Month and the Next Monday
For the last day of the current month we do not need to know whether the month has 28, 29, 30 or 31 days. The adjuster asks the calendar for us, so leap years are handled too.
Notice the difference between next and nextOrSame. The question says the next Monday after a given date, not the same day, so next() is correct: when the date is already a Monday, it jumps a full week ahead. nextOrSame() would return the date itself. Mixing these two up is a common source of off-by-a-week bugs in scheduling code, so it is worth saying out loud which one the requirement means.
3Task 3: Counting the Days Between Two Dates
ChronoUnit.DAYS.between(start, end) returns a long with the number of days from start up to end. The start is included and the end is excluded, which is the same convention as array indexes and most Java ranges. If end is before start, the result is negative.
Period.between answers a different question. It splits the gap into years, months and days, the way a person would describe an age. So getDays() on that period returns only the leftover days after whole months are taken out, which is 1 here. Many bugs come from calling Period.between(...).getDays() when the total number of days was wanted. When the requirement says the number of days, we use ChronoUnit.DAYS.between and decide explicitly whether the end date counts.
4The Immutability Trap
Every java.time type is immutable. A method such as plusDays or with never changes the object it is called on; it returns a new object. This is what makes these types safe to share between threads and to use as map keys, but it surprises people who are used to the old java.util.Calendar, whose add() method changed the calendar in place.
The first call compiles and runs without any warning from the compiler, and the date simply never moves. In a loop this turns into an infinite loop, because the loop variable never reaches its target. Static analysis tools flag ignored return values of these methods, but the habit to build is simple: every java.time call that returns a date must be assigned.
5Task 4: Thirty Business Days After Today
A business day here means Monday to Friday, and the question says to ignore holidays. The clearest solution is a loop that moves forward one day at a time and counts a day only when it is not a Saturday or a Sunday. We stop when we have counted 30.
- d = start, counted = 0
- d = d.plusDays(1)move first, so the start date never counts
- Is d a Saturday or Sunday?Yesskip, go back to step 2Nocounted = counted + 1
- counted == n?Yesreturn dNogo back to step 2
4 steps in order. Play them, or step through them one at a time.
The reason we start by moving to the next day before checking is that today itself should not count as one of the 30 days. Thirty business days is exactly six full weeks, so from a Wednesday we land on a Wednesday 42 calendar days later, which is a quick sanity check for the result.
Wrapping the loop in a TemporalAdjuster lets callers write date.with(plus30), in the same style as the built-in adjusters. The loop is fast for counts like 30 and easy to extend when holidays join the requirement: we just add a check against a set of holiday dates.
6Follow-up questions
Why is LocalDate.plusMonths(1) on 31 January not 31 February?
Because 31 February does not exist. plusMonths first adds the month to get February 31, then sees the day is not valid for that month and clamps it to the last valid day, so 2026-01-31 plus one month is 2026-02-28, and in a leap year it is the 29th. This means adding one month twice is not always the same as adding two months: 31 January plus one month is 28 February, and plus another month is 28 March, while 31 January plus two months is 31 March. For billing cycles we usually anchor every date to the original start date and add n months to it, rather than chaining additions.
Is LocalDate.now() safe to use in this code?
It works, but it reads the system default time zone and the real clock, which makes the code hard to test and can give a different date on servers in other zones. Better code takes a Clock as a dependency and calls LocalDate.now(clock). In tests we pass Clock.fixed(...) so today is always the same date, and in production we pass a clock with the business time zone, such as Europe/Paris, so that today means the same thing for everyone.
7Conclusion
In this answer, we saw that java.time gives a ready-made adjuster for the last day of the month and for the next Monday, where next skips the given date and nextOrSame keeps it. We counted days with ChronoUnit.DAYS.between, which excludes the end date, and saw that Period.between answers a different question. We wrote a business-day loop that moves forward and counts only weekdays, wrapped it as a TemporalAdjuster, and kept in mind that every java.time value is immutable, so each result must be assigned.
Open with
For the last day of the month I would write date.with(TemporalAdjusters.lastDayOfMonth()), which handles month lengths and leap years for me. For the next Monday after a date I use TemporalAdjusters.next(DayOfWeek.MONDAY); if the date is already a Monday it jumps a week ahead, and if the requirement wanted the same day to count I would use nextOrSame instead.
Then, if they want more
For the days between two dates I use ChronoUnit.DAYS.between(start, end), which includes the start and excludes the end, so I add one if both ends should count. I would not use Period.between for that, because its getDays only gives the leftover days after whole months.
For 30 business days I write a small loop: move to the next day first, check getDayOfWeek, count it only if it is not Saturday or Sunday, and stop at 30. I would wrap that in a TemporalAdjuster so callers can write today.with(plusBusinessDays(30)), and it is easy to add a holiday set later.
One thing I always watch for is immutability: plusDays and with return new objects, so if I forget to assign the result the date never changes, and in a loop that becomes an infinite loop. I would also pass a Clock into the code instead of calling LocalDate.now() directly, so tests can fix today's date.
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 "Using java.time, compute: the last day of the current month, the next Monday after a...": https://scrivio-interview-library.netlify.app/java/date-and-time/32d7a6a3f4/