Java Day End getMonthFirstDays(Date startDate, Date endDate)

Here you can find the source of getMonthFirstDays(Date startDate, Date endDate)

Description

get Month First Days

License

Apache License

Declaration

public static List<Date> getMonthFirstDays(Date startDate, Date endDate) 

Method Source Code

//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();
    }
}

Related

  1. getMonthCount(Date startDate, Date endDate)
  2. getMonthEnd(Date date)
  3. getMonthEndDate(Date date)
  4. getMonthEndDate(int year, int month)
  5. getMonthEndDate(Long day)
  6. getMonths(Date end, Date start)
  7. getMonthsBetweenBeginDateAndEndDate(Date beginDate, Date endDate)
  8. getMonthSpan(Date begin, Date end)
  9. getMonthSpan(Date start, Date end)