Here you can find the source of getLastDay(java.util.Date dt)
public static int getLastDay(java.util.Date dt)
//package com.java2s; //License from project: Open Source License import java.text.SimpleDateFormat; import java.util.Calendar; public class Main { private final static int[] SOLAR_MONTH_ARRAY = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; public static int getLastDay(String dt) { if (dt.length() < 6) throw new IllegalArgumentException("date type -- yyyyMM, yyyyMMdd, yyyyMMddHHmmss"); int year = Integer.parseInt(dt.substring(0, 4)); int month = Integer.parseInt(dt.substring(4, 6)); if (isLeapYear(year)) return (month == 2) ? 29 : SOLAR_MONTH_ARRAY[month - 1]; else//from w w w . jav a2 s .co m return SOLAR_MONTH_ARRAY[month - 1]; } public static int getLastDay(java.sql.Date dt) { return getLastDay(convertFormat(dt, "yyyyMM")); } public static int getLastDay(java.sql.Timestamp dt) { return getLastDay(convertFormat(dt, "yyyyMM")); } public static int getLastDay(java.util.Date dt) { return getLastDay(convertFormat(dt, "yyyyMM")); } public static boolean isLeapYear(int year) { return (year % 400 == 0 || year % 100 != 0 && year % 4 == 0); } public static String convertFormat(long dt, String formatter) { return convertFormat(toUtilDate(dt), formatter); } /** * conver date format * @param dt String (yyyyMMdd or yyyyMMddHHmmss) * @param formatter String (yyyyMMdd.....) * @return String conver date format */ public static String convertFormat(String dt, String formatter) { if (dt == null) { // throw new IllegalArgumentException("dt can not be null"); return ""; } dt = dt.trim(); int len = dt.length(); if (!(len == 8 || len == 14)) { // throw new IllegalArgumentException("dt length must be 8 or 14 (yyyyMMdd or yyyyMMddHHmmss)"); return dt; } if (dt.length() == 8) dt += "000000"; SimpleDateFormat sdf = new SimpleDateFormat(formatter); return sdf.format(getDate(dt)); } /** * conver date format * @param dt java.sql.Date * @param formatter String (yyyyMMdd.....) * @return conver date format */ public static String convertFormat(java.sql.Date dt, String formatter) { return convertFormat((java.util.Date) dt, formatter); } /** * conver date format * @param dt java.util.Date dt * @param formatter String formatter (yyyyMMdd.....) * @return String */ public static String convertFormat(java.util.Date dt, String formatter) { SimpleDateFormat sdf = new SimpleDateFormat(formatter); return sdf.format(dt); } public static java.util.Date toUtilDate(long dt) { return new java.util.Date(dt); } /** * convert date String (yyyyMMdd or yyyyMMddHHmmss) to java.util.Date * @param dt String (yyyyMMdd or yyyyMMddHHmmss) * @return java.util.Date */ public static java.util.Date toUtilDate(String dt) { if (dt == null) { throw new IllegalArgumentException("dt can not be null"); } int len = dt.length(); if (!(len == 8 || len == 14)) { throw new IllegalArgumentException("dt length must be 8 or 14 (yyyyMMdd or yyyyMMddHHmmss)"); } if (dt.length() == 8) { dt += "000000"; } return getDate(dt); } /** * yyyyMMddHHmmss --> java.util.Date * @param String dt * @return java.util.Date */ private static java.util.Date getDate(String dt) { Calendar cal = Calendar.getInstance(); cal.set(Integer.valueOf(dt.substring(0, 4)).intValue(), Integer.valueOf(dt.substring(4, 6)).intValue() - 1, Integer.valueOf(dt.substring(6, 8)).intValue(), Integer.valueOf(dt.substring(8, 10)).intValue(), Integer.valueOf(dt.substring(10, 12)).intValue(), Integer.valueOf(dt.substring(12, 14)).intValue()); return cal.getTime(); } }