Java Month Format getDisplayMonth(Date time, int monthBefore, int monthAfter, SimpleDateFormat format)

Here you can find the source of getDisplayMonth(Date time, int monthBefore, int monthAfter, SimpleDateFormat format)

Description

get Display Month

License

Apache License

Declaration

public static List getDisplayMonth(Date time, int monthBefore, int monthAfter, SimpleDateFormat format) 

Method Source Code

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

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class Main {
    public static List getDisplayMonth(Date time, int monthBefore, int monthAfter, SimpleDateFormat format) {
        ArrayList result = new ArrayList();
        Calendar calendar = Calendar.getInstance();
        if (null != time) {
            calendar.setTimeInMillis(time.getTime());
        }//from   w  ww .  java  2s. c  om
        SimpleDateFormat tmpformat = format;
        if (null == tmpformat) {
            tmpformat = new SimpleDateFormat("yyyy/MM");
        }
        Calendar calendartmp = Calendar.getInstance();

        for (int i = monthBefore; i <= monthAfter; i++) {
            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(tmpformat.format(new Date(calendartmp.getTimeInMillis())));
        }
        return result;
    }

    public static List getDisplayMonth(Date from, Date to, String pattern) {
        ArrayList result = new ArrayList();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(from);
        Calendar calendarTo = Calendar.getInstance();
        calendarTo.setTime(getMonthLastDay(to));
        if (pattern == null) {
            pattern = "yyyy/MM";
        }
        SimpleDateFormat formatter = new SimpleDateFormat(pattern, Locale.ENGLISH);

        while (calendar.before(calendarTo)) {
            result.add(formatter.format(calendar.getTime()));
            calendar.add(2, 1);
        }
        return result;
    }

    public static List getDisplayMonth(Date time, int monthBefore, int monthAfter) {
        return getDisplayMonth(time, monthBefore, monthAfter, null);
    }

    public static List getDisplayMonth(int monthBefore, int monthAfter) {
        return getDisplayMonth(null, monthBefore, monthAfter);
    }

    public static List getDisplayMonth(int monthBefore, int monthAfter, String format) {
        return getDisplayMonth(null, monthBefore, monthAfter, new SimpleDateFormat(format, Locale.ENGLISH));
    }

    public static Date getMonthLastDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        if (null != date) {
            calendar.setTime(date);
        }
        calendar.set(5, calendar.getActualMaximum(5));
        calendar.set(11, 23);
        calendar.set(12, 59);
        calendar.set(13, 59);
        calendar.set(14, 999);
        return calendar.getTime();
    }
}

Related

  1. formatMonth(String dateOrign)
  2. formatMonthDay(int decimal)
  3. formatMonthlyPeriod(String month)
  4. getAfterByNMonth(String format, int months)
  5. getCurrMonthFirstDaySlashFormat()
  6. getFirstDayOfNextMonth(String dateFormat)
  7. getFormatCurMonthAsYYYYMM()
  8. getLastYearMonthYYYYMM()
  9. getMonthFormat(String dateString)