Java Month Get getAllMonths(String date1, String date2)

Here you can find the source of getAllMonths(String date1, String date2)

Description

get All Months

License

Apache License

Declaration

public static List<String> getAllMonths(String date1, String date2) 

Method Source Code


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

import java.text.DateFormat;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import java.util.HashMap;
import java.util.List;

import java.util.Map;

public class Main {
    private static final String dateFormat3 = "yyyy-MM-dd";
    private static final String dateFormat5 = "yyyy-MM";
    private static final Map<String, DateFormat> DFS = new HashMap<String, DateFormat>();

    public static List<String> getAllMonths(String date1, String date2) {
        date1 = date2String1(string2Date(date1)) + "-01";
        date2 = date2String1(string2Date(date2)) + "-01";
        List list = new ArrayList();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar startDay = Calendar.getInstance();
        Calendar endDay = Calendar.getInstance();
        try {/*from   ww w.  j a  v  a  2 s. c om*/
            startDay.setTime(format.parse(date1));
            endDay.setTime(format.parse(date2));
        } catch (ParseException e) {
        }
        //      if (startDay.compareTo(endDay) >= 0) {
        //         return list;
        //      }
        Calendar currentPrintDay = startDay;
        while (true) {
            if (currentPrintDay.compareTo(endDay) > 0) {
                break;
            }
            list.add(format.format(currentPrintDay.getTime()));
            currentPrintDay.add(Calendar.MONTH, 1);
        }
        return list;
    }

    public static String date2String1(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat(dateFormat5);
        String dateString = formatter.format(date);
        return dateString;
    }

    public static Date string2Date(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat(dateFormat3);
        ParsePosition pos = new ParsePosition(0);
        Date date = formatter.parse(strDate, pos);
        return date;
    }

    public static Date parse(String source, String pattern) {
        if (source == null) {
            return null;
        }
        Date date;
        try {
            date = getFormat(pattern).parse(source);
        } catch (ParseException e) {
            return null;
        }
        return date;
    }

    public static String format(Date date, String pattern) {
        if (date == null) {
            return null;
        }
        return getFormat(pattern).format(date);
    }

    public static DateFormat getFormat(String pattern) {
        DateFormat format = DFS.get(pattern);
        if (format == null) {
            format = new SimpleDateFormat(pattern);
            DFS.put(pattern, format);
        }
        return format;
    }
}

Related

  1. currentMonth()
  2. currentMonthStr()
  3. getAktMonthName()
  4. getBeforeMonth(int amount)
  5. getBeforeOrAfterDate(String strDate, int months)
  6. getCurMonth2Long()
  7. getCurrMonthLastDay()