Here you can find the source of getLastDayInMonth(int year, int month)
public static int getLastDayInMonth(int year, int month)
//package com.java2s; /**/*from w w w .ja v a 2 s.com*/ Copyright 2014 Jens Glufke Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. * ----------------------------------------------------------------------- * */ import java.util.Calendar; import java.util.HashMap; public class Main { /** * key - year * value - day count */ private static HashMap<Integer, Integer> daysOfFebruary = new HashMap<Integer, Integer>(); public static int getLastDayInMonth(int year, int month) { int lLastDay = -1; switch (month) { case 0: // Jan case 2: // Mar case 4: // May case 6: // Jul case 7: // Aug case 9: // Oct case 11: // Dec lLastDay = 31; break; case 1: // Feb Integer lDayCount = daysOfFebruary.get(year); if (lDayCount == null) { Calendar lCalendar = Calendar.getInstance(); lCalendar.set(year, 2, 1); lCalendar.add(Calendar.DATE, -1); lDayCount = lCalendar.get(Calendar.DAY_OF_MONTH); daysOfFebruary.put(year, lDayCount); } lLastDay = lDayCount; break; case 3: // Apr case 5: // Jun case 8: // Sep case 10: // Nov lLastDay = 30; break; default: throw new IllegalArgumentException("month out of range:" + month); } return lLastDay; } }