0

I need to determine the number of days in the month represented by the month portion of a java.time.ZonedDateTime. I've come up with three ways that appear to work. Given:

ZonedDateTime date = _some date_;

Option 1:

int daysInMonth = date.getMonth().length(date.getChronology().isLeapYear(date.getYear()));

Option 2:

int daysInMonth = date.getMonth().length(date.toLocalDate().isLeapYear());

Option 3:

int daysInMonth = YearMonth.from(date).lengthOfMonth();

I discovered option 3 while checking this question for duplicates (Number of days in particular month of particular year?). Am I missing any other options? Is one of these options superior to another? Under what circumstances will that superiority manifest?

If I'm reading things correctly, the first one appears to be the only one capable of supporting chronologies other than IsoChronology. That's not important for the use case at hand, but I do like to write flexible, reusable code, and I'd like for this to not fall down in other situations. I'm not an expert on those alternative chronologies, and I don't know if they have leap years or anything else that might cause the length of a month to vary. Heck, I don't even know if they have months.

EDIT: Option 4, per @Ole V.V.'s answer, below:

int daysInMonth = date.toLocalDate().lengthOfMonth();
JakeRobb
  • 1,711
  • 1
  • 17
  • 32
  • Apparently I have a downvote and two votes to close. Would anyone care to fill me in on the problem? ā€“ JakeRobb Nov 04 '19 at 15:43

1 Answers1

4
    int daysInMonth = date.toLocalDate().lengthOfMonth();

I understand your confusion. There are several ways to do this, and the above is not the only good way. In particular Iā€™m pretty fond of your option 3 too. The options explicitly involving isLeapYear() are not to my taste, I find them too low-level, too manual.

Under what circumstances will that superiority manifest?

Readability and absence of surprises are the kings. From there your judgement is at least as good as mine. Chances are that you know the people who are going to maintain your code better than I do.

You may also take a step back and ask yourself what you need the number of days in a month for. I am mentioning this because one typical use would be for calculations that java.time can itself do better than you can, like adding 24 days to a date, finding the count of days between two dates in different months or finding out which day of the year a certain date is. java.time has got methods for all of these and many more.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • Actually, the point is that you don't know who is going to maintain your code in the future, as you cannot know the future, so you write the code to be easily readable and understandable by any competent (we hope) future programmer. ā€“ Andreas Nov 02 '19 at 17:32
  • 1
    Added this as option 4. Thanks for your thoughts on maintainability; this is code for personal use, so I'm the only maintainer. Assuming I get a conclusive answer here, I'll comment next to the code with a link to said answer in order to provide clarity to my future self and/or any unexpected future coding partners. ā€“ JakeRobb Nov 02 '19 at 17:45