Here you can find the source of getMonthLength(String countDate)
public static long getMonthLength(String countDate) throws ParseException
//package com.java2s; //License from project: Apache License import java.text.ParseException; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; public class Main { public static long getMonthLength(String countDate) throws ParseException { String firstDay = countDate.substring(0, countDate.length() - 2) + "01"; Date startDate = strToDate(firstDay); Date endDate = getNextMonth(startDate, new Long(1)); long startDateTime = startDate.getTime(); long endDateTime = endDate.getTime(); long dayTime = 24 * 60 * 60 * 1000; long days = (endDateTime - startDateTime) / dayTime; return days; }/*from w ww.jav a 2 s . c om*/ public static Date strToDate(String date) throws ParseException { java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyyMMdd"); java.util.Date d = sdf.parse(date); return d; } public static Date getNextMonth(Date dt, Long n) { Calendar cal = new GregorianCalendar(); cal.setTime(dt); Calendar firstCal = new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + n.intValue(), 1); if (firstCal.getActualMaximum(Calendar.DAY_OF_MONTH) < cal.get(Calendar.DAY_OF_MONTH)) { return new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + n.intValue(), firstCal.getActualMaximum(Calendar.DAY_OF_MONTH)).getTime(); } else { return new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + n.intValue(), cal.get(Calendar.DAY_OF_MONTH)).getTime(); } } }