Java Date Before getMonth(Date time, int monthBefore, int monthAfter)

Here you can find the source of getMonth(Date time, int monthBefore, int monthAfter)

Description

get Month

License

Apache License

Declaration

public static Date[] getMonth(Date time, int monthBefore, int monthAfter) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

public class Main {
    public static Date[] getMonth(Date time, int monthBefore, int monthAfter) {
        ArrayList result = new ArrayList();
        Calendar calendar = Calendar.getInstance();
        if (null != time) {
            calendar.setTimeInMillis(time.getTime());
        }/*from  w  w  w.  ja  v a 2  s.  c om*/
        for (int i = monthBefore; i <= monthAfter; i++) {
            Calendar calendartmp = Calendar.getInstance();
            calendartmp.setTimeInMillis(calendar.getTimeInMillis());
            calendartmp.set(5, 1);
            calendartmp.set(11, 0);
            calendartmp.set(12, 0);
            calendartmp.set(13, 0);
            calendartmp.set(14, 0);
            calendartmp.add(2, i);
            result.add(new Date(calendartmp.getTimeInMillis()));
        }
        return (Date[]) (Date[]) result.toArray(new Date[monthAfter - monthBefore + 1]);
    }

    public static Date[] getMonth(Date startMonth, Date endMonth) {
        ArrayList result = new ArrayList();
        Calendar calendar = Calendar.getInstance();
        Calendar calendarEnd = Calendar.getInstance();
        if (null != startMonth) {
            calendar.setTimeInMillis(startMonth.getTime());
        }
        if (null != endMonth) {
            calendarEnd.setTimeInMillis(endMonth.getTime());
        }
        while (!calendar.after(calendarEnd)) {
            result.add(new Date(calendar.getTimeInMillis()));
            calendar.add(2, 1);
        }

        return (Date[]) (Date[]) result.toArray(new Date[result.size()]);
    }

    public static Date[] getMonth(int monthBefore, int monthAfter) {
        return getMonth(null, monthBefore, monthAfter);
    }
}

Related

  1. getDateTimeAfterOfBeforeHour(Integer AfterOfBeforeHour)
  2. getDayBeforeOrAfter2(Date time, int days)
  3. getDayFormSomeDay(Date date, int n, boolean before)
  4. getDistanceMonthOfTwoDate(Date before, Date after)
  5. getMinuteBefore(Date date, int minute)
  6. getMonthBefore(Date currentDate, int numberMonthBefore)
  7. getMonthStartDateBeforeCurrent(int monthNum, Date current)
  8. getOneHourBefore(Date date)
  9. getPassedMonthBetweenDate(Date before, Date after)