How do I obtain the number of days within a given month using Joda-Time?

Manuel Araoz picture Manuel Araoz · Sep 22, 2010 · Viewed 27.1k times · Source
30 days hath September,
   April, June and November,
 All the rest have 31,
   Excepting February alone
(And that has 28 days clear,
   With 29 in each leap year).

Can I obtain this info anagrammatically? (I don't mean the poem, of course)

Answer

Erick Robertson picture Erick Robertson · Sep 22, 2010

If you have a DateTime object which represents a value in the month, then it's pretty straightforward. You get the dayOfMonth property from that DateTime object and get the maximum value of the property. Here is a sample function:

public static int daysOfMonth(int year, int month) {
  DateTime dateTime = new DateTime(year, month, 14, 12, 0, 0, 000);
  return dateTime.dayOfMonth().getMaximumValue();
}