-7

How do I calculate how many days are left in the month we are in, in Java?

For example, today is the 5th of November, the result would be:

25 days left for the end of the month.

How to do this?

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Mussum
  • 21
  • 1
  • 10
  • 1
    How would you do it if all you had were a piece of paper and a pencil? How would you determine how many days each month had? How would you account for leap years? Think about it instead of asking for help right away. – MarsAtomic Nov 05 '20 at 21:02
  • 8
    @Abra Lack of research effort is not a reason to close a question. You can down-vote the question if you want. – cigien Nov 05 '20 at 21:14
  • 7
    @Abra You **are** wrong. "No effort" is not a close reason. You may vote to reopen (if this isn't a duplicate question). If you find a duplicate, you should comment it so that people don't vote to reopen a duplicate page. – mickmackusa Nov 05 '20 at 21:15
  • 1
    @mickmackusa please enlighten me. What **is** a reason for voting to close a question that is **not** one of the available options? – Abra Nov 06 '20 at 09:23

1 Answers1

1

In Java there's been for a while Java Time API, which allows to get the number of days in the given month and calculate the difference using java.time.temporal.ChronoUnit.DAYS:

LocalDate today = LocalDate.now();
LocalDate endOfMonth = today.withDayOfMonth(today.lengthOfMonth());
long daysBetween = DAYS.between(today, endOfMonth);
Nowhere Man
  • 19,170
  • 9
  • 17
  • 42
  • 1
    How do you know which year the input will be about? – Braiam Nov 05 '20 at 21:48
  • 2
    @Braiam To my knowledge all languages that implement `.now()` do so by comparing it to the local system's clock and calendar. So to answer your question, OP will know the year by what is returned by `.now()`. – TylerH Nov 05 '20 at 21:49
  • @TylerH that presume that is the current year, that's not clear from the OP posts. I'm asking Alex, because he presumed that the input will always be today. That's not what OP asks about, but an example. – Braiam Nov 05 '20 at 22:01
  • 2
    @Braiam both the phrase "the month we are in" and "today is November 5th" (which matches the current .now() date value) indicate fairly clearly, in my opinion, that OP does mean the current year (and month). – TylerH Nov 05 '20 at 22:22
  • @TylerH again, that's your interpretation. i do not interpret it that way. And, as seen in the comment to the OP, I'm not the only one. – Braiam Nov 05 '20 at 22:46
  • @Braiam What comment? The one you posted yourself? It's also not really an "interpretation" so much as just a plain reading of the question. – TylerH Nov 05 '20 at 22:54
  • 1
    Good answer. As a matter of taste I tend to find `today.with(TemporalAdjusters.lastDayOfMonth​())` more natural. – Ole V.V. Nov 05 '20 at 23:24
  • @Braiam To be strict the *we* in *the month we are in* is not clear. The month doesn’t change at the same moment in all time zones, and it is never the same date in all time zones. So we need a time zone for the operation. In my interpretation with a time zone we’re all set, but of course only Mussum, the OP, has the authoritative answer to that. – Ole V.V. Nov 06 '20 at 05:38
  • @OleV.V. which is why I voted to close as too broad, and [asked about it](https://stackoverflow.com/questions/64705000/how-to-calculate-remaining-days-of-the-month/64705315?noredirect=1#comment114407817_64705000) – Braiam Nov 06 '20 at 11:00