Here you can find the source of getMonthFirstDays(Date startDate, Date endDate)
public static List<Date> getMonthFirstDays(Date startDate, Date endDate)
//package com.java2s; //License from project: Apache License import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; public class Main { public static List<Date> getMonthFirstDays(Date startDate, Date endDate) { if (endDate != null && !startDate.before(endDate)) { throw new RuntimeException("startDate must before endDate."); }/* w w w.ja v a 2s. c om*/ List<Date> monthFirstDayList = new ArrayList<Date>(); Date previousMonthFirstDay = getPreviousMonthFirstDay(endDate); for (; !previousMonthFirstDay .before(startDate); previousMonthFirstDay = getPreviousMonthFirstDay(previousMonthFirstDay)) { monthFirstDayList.add(previousMonthFirstDay); } return monthFirstDayList; } public static Date getPreviousMonthFirstDay(Date day) { Calendar firstDayOfMonth = Calendar.getInstance(); firstDayOfMonth.setTime(day); firstDayOfMonth.add(Calendar.MONTH, -1); firstDayOfMonth.set(Calendar.DAY_OF_MONTH, 1); firstDayOfMonth.set(Calendar.HOUR_OF_DAY, 0); firstDayOfMonth.set(Calendar.MINUTE, 0); firstDayOfMonth.set(Calendar.SECOND, 0); return firstDayOfMonth.getTime(); } }