0

Given a YearMonth (from org.joda.time.YearMonth), is there a easy way to get the number of days in that month?

I saw this thread Number of days in particular month of particular year? and it works fine with the following.

  1. Create a Calendar object
  2. Call getActualMaximum with Calendar.DAY_OF_MONTH

Like this:

YearMonth yearMonth = new YearMonth();
Calendar cal = new GregorianCalendar(yearMonth.getYear(), yearMonth.getMonthOfYear(), 1);
int daysInMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH);

But I am wondering if there is some utility function in joda that I can just get the days in month directly?

Community
  • 1
  • 1
Yuchen
  • 30,852
  • 26
  • 164
  • 234

1 Answers1

0

Can be done with this:

YearMonth yearMonth = new YearMonth();
yearMonth.toLocalDate(1).dayOfMonth().getMaximumValue();

I haven't tried this and obviously this SO is closed, but please checkout the solution suggested by nooruddin-khorasi below in the comments too:

YearMonth yearMonth = new YearMonth();
yearMonth.lengthOfMonth();
Yuchen
  • 30,852
  • 26
  • 164
  • 234
  • A much better way: `YearMonth yearMonth = new YearMonth(); yearMonth.lengthOfMonth();` – noor Jun 19 '17 at 22:57
  • Hey @nooruddinkhorasi, was looking for length of days for this. – Yuchen Jun 20 '17 at 02:00
  • OP was looking for a way to find number of days in any given month. `lengthOfMonth()` gives the intended number. – noor Jun 20 '17 at 03:13
  • 1
    Hey @nooruddinkhorasi, I misunderstood you original comment. Added your suggestion in the answer too. Thanks! – Yuchen Jun 20 '17 at 15:12