Here you can find the source of getMonthRange(String strBeginDate, String strEndDate, String pattern)
public static List<String> getMonthRange(String strBeginDate, String strEndDate, String pattern)
//package com.java2s; //License from project: Open Source License import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; public class Main { /**/*from w w w. ja v a 2 s . c o m*/ * default date format pattern */ public final static String DATE_FORMAT = "yyyy-MM-dd"; @SuppressWarnings("deprecation") public static List<String> getMonthRange(int year, int month) { Calendar calendar = getDefaultCalendar(); calendar.set(Calendar.YEAR, year); calendar.set(Calendar.MONTH, month - 1); calendar.set(Calendar.DAY_OF_MONTH, 1); List<String> result = new ArrayList<String>(); Date date = calendar.getTime(); while (date.getMonth() == calendar.get(Calendar.MONTH)) { result.add(formatDate(date)); date = addDate(date, 1); } return result; } public static List<String> getMonthRange(String strBeginDate, String strEndDate, String pattern) { List<String> ret = new ArrayList<String>(); Date beginDate = parseDate(strBeginDate); Date endDate = parseDate(strEndDate); String beginFormat = formatDate(beginDate, pattern); String endFormat = formatDate(endDate, pattern); ret.add(beginFormat); while (!beginFormat.equals(endFormat)) { beginDate = addMonth(beginDate, 1); beginFormat = formatDate(beginDate, pattern); ret.add(beginFormat); } return ret; } /** * get the default calendar * * @return the calendar instance */ public static Calendar getDefaultCalendar() { Calendar calendar = Calendar.getInstance(); calendar.setFirstDayOfWeek(Calendar.MONDAY); return calendar; } public static int getMonth(Date date) { Calendar calendar = getCalendarFromDate(date); return calendar.get(Calendar.MONTH) + 1; } /** * format date with the default pattern * * @param date the date that want to format to string * @return the formated date */ public static String formatDate(Date date) { SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT); return format.format(date); } /** * format date with the given pattern * * @param date the date that want to format to string * @param pattern the formated pattern * @return the formated date */ public static String formatDate(Date date, String pattern) { if (date == null) { return null; } SimpleDateFormat format = new SimpleDateFormat(pattern); return format.format(date); } /** * add day to the date * * @param date the added date * @param number the number to add to the date * @return the added date */ public static Date addDate(Date date, int number) { Calendar calendar = getDefaultCalendar(); calendar.setTime(date); calendar.add(Calendar.DATE, number); return calendar.getTime(); } /** * parse date with the default pattern * * @param date string date * @return the parsed date */ public static Date parseDate(String date) { SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT); try { return format.parse(date); } catch (ParseException e) { return new Date(); } } public static Date parseDate(String date, String pattern) { SimpleDateFormat format = new SimpleDateFormat(pattern); try { return format.parse(date); } catch (ParseException e) { return new Date(); } } public static Date addMonth(Date date, int number) { Calendar calendar = getDefaultCalendar(); calendar.setTime(date); calendar.add(Calendar.MONTH, number); return calendar.getTime(); } /** * get calendar from date * * @param date the passing date * @return the calendar instance */ public static Calendar getCalendarFromDate(Date date) { Calendar calendar = getDefaultCalendar(); calendar.setTime(date); return calendar; } }