Java - Write code to get Date List between two date

Requirements

Write code to get Date List between two date

Demo

//package com.book2s;
import java.text.DateFormatSymbols;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

public class Main {
    public static void main(String[] argv) {
        String startTime = "1999-10-01";
        String endTime = "2000-01-01";
        System.out.println(getDateList(startTime, endTime));
    }/*w w  w . j  a  v  a2 s  .  co  m*/

    public static List<String> getDateList(String startTime, String endTime) {
        Calendar c_begin = new GregorianCalendar();
        Calendar c_end = new GregorianCalendar();
        DateFormatSymbols dfs = new DateFormatSymbols();
        String[] weeks = dfs.getWeekdays();
        String[] start = startTime.split("-");
        String[] end = endTime.split("-");
        c_begin.set(Integer.parseInt(start[0]),
                Integer.parseInt(start[1]) - 1, Integer.parseInt(start[2]));
        c_end.set(Integer.parseInt(end[0]), Integer.parseInt(end[1]) - 1,
                Integer.parseInt(end[2]));
        int count = 1;
        List<String> list = new ArrayList<String>();
        if (!c_begin.equals(c_end)) {
            c_end.add(Calendar.DAY_OF_YEAR, 1);
        } else {
            list.add(new java.sql.Date(c_begin.getTime().getTime()) + "");
        }

        while (c_begin.before(c_end)) {
            list.add(new java.sql.Date(c_begin.getTime().getTime()) + "");
            if (c_begin.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
                count++;
            }
            c_begin.add(Calendar.DAY_OF_YEAR, 1);
        }
        return list;

    }
}

Related Exercise