Here you can find the source of getDaysOfMonth(Date startdate, Date enddate, String month)
static public int getDaysOfMonth(Date startdate, Date enddate, String month) throws Exception
//package com.java2s; //License from project: Open Source License import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; public class Main { /**/*from w ww .j a va 2 s . c o m*/ * format pattern : yyyy-MM-dd */ public static final SimpleDateFormat FORMAT_YYYY_MM_DD = new SimpleDateFormat("yyyy-MM-dd"); static public int getDaysOfMonth(Date startdate, Date enddate, String month) throws Exception { Calendar cs = Calendar.getInstance(); cs.setTime(startdate); Calendar ce = Calendar.getInstance(); ce.setTime(enddate); //int startmonth = startdate.getMonth() + 1; //int endmonth = enddate.getMonth() + 1; int startmonth = cs.get(Calendar.MONTH) + 1; int endmonth = ce.get(Calendar.MONTH) + 1; int m = Integer.parseInt(month); //int day = getLastDay(String.valueOf(startdate.getYear()), month) //.getDate(); Date ld = getLastDay(String.valueOf(cs.get(Calendar.YEAR)), month); Calendar c = Calendar.getInstance(); c.setTime(ld); int day = c.get(Calendar.DAY_OF_MONTH); if ((startmonth < m) && (m < endmonth)) { return day; } else if (m == startmonth) { //return day - startdate.getDate() + 1; return day - cs.get(Calendar.DAY_OF_MONTH); } else if (m == endmonth) { //return enddate.getDate(); return ce.get(Calendar.DAY_OF_MONTH); } return 0; } static public Date getLastDay(String year, String month) throws ParseException { Date date = FORMAT_YYYY_MM_DD.parse(year + "-" + month + "-1"); Calendar scalendar = new GregorianCalendar(); scalendar.setTime(date); scalendar.add(Calendar.MONTH, 1); scalendar.add(Calendar.DATE, -1); date = scalendar.getTime(); return date; } static public Date getLastDay(int year, int month) throws ParseException { Date date = FORMAT_YYYY_MM_DD.parse(year + "-" + month + "-1"); Calendar scalendar = new GregorianCalendar(); scalendar.setTime(date); scalendar.add(Calendar.MONTH, 1); scalendar.add(Calendar.DATE, -1); date = scalendar.getTime(); return date; } static public Date getLastDay(Date date) { Calendar c = Calendar.getInstance(); c.setTime(date); c.set(Calendar.DAY_OF_MONTH, 1); c.roll(Calendar.DATE, false); return c.getTime(); } }