How to know how many days has particular month of particular year?
String date = "2010-01-19";
String[] ymd = date.split("-");
int year = Integer.parseInt(ymd[0]);
int month = Integer.parseInt(ymd[1]);
int day = Integer.parseInt(ymd[2]);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR,year);
calendar.set(Calendar.MONTH,month);
int daysQty = calendar.getDaysNumber(); // Something like this
You can use
Calendar.getActualMaximum
method:Code for java.util.Calendar
If you have to use
java.util.Calendar
, I suspect you want:Code for Joda Time
Personally, however, I'd suggest using Joda Time instead of
java.util.{Calendar, Date}
to start with, in which case you could use:Note that rather than parsing the string values individually, it would be better to get whichever date/time API you're using to parse it. In
java.util.*
you might useSimpleDateFormat
; in Joda Time you'd use aDateTimeFormatter
.java.time.LocalDate
From Java 1.8, you can use the method
lengthOfMonth
onjava.time.LocalDate
: