I highly recommend that you avoid the built-in date and time APIs in Java.
Use Joda Time instead . This library is similar to the one that (hopefully!) Will turn into Java 7 and will be much more pleasant to use than the built-in API.
Now, the main problem that you want to know about the number of days in a particular month?
EDIT: here is the code (with sample):
import org.joda.time.*;
import org.joda.time.chrono.*;
public class Test
{
public static void main(String[] args)
{
System.out.println(getDaysInMonth(2009, 2));
}
public static int getDaysInMonth(int year, int month)
{
Chronology chrono = ISOChronology.getInstance();
DateTimeField dayField = chrono.dayOfMonth();
LocalDate monthDate = new LocalDate(year, month, 1);
return dayField.getMaximumValue(monthDate);
}
}
source
share