Java Date get Date List between two date values

Description

Java Date get Date List between two date values

import java.text.DateFormatSymbols;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

public class Main {
    public static void main(String[] argv) {
      Calendar start = Calendar.getInstance();
      start.set(2020, 01, 01);/*from   w  w w  .  j  av  a 2 s .  co  m*/
      
      Calendar end = Calendar.getInstance();
      end.set(2020, 01, 4);
        Date startDate = start.getTime();
        Date endDate = end.getTime();
        System.out.println(getDateList(startDate, endDate));
    }

    public static List<Date> getDateList(Date start, Date end) {
        Calendar c_begin = new GregorianCalendar();
        Calendar c_end = new GregorianCalendar();
        c_begin.setTime(start);
        c_end.setTime(end);
        List<Date> list = new ArrayList<Date>();
        if (!c_begin.equals(c_end)) {
            c_end.add(Calendar.DAY_OF_YEAR, 1);
        } else {
            list.add(new Date(c_begin.getTime().getTime()));
        }

        while (c_begin.before(c_end)) {
            list.add(new Date(c_begin.getTime().getTime()));
            c_begin.add(Calendar.DAY_OF_YEAR, 1);
        }
        return list;
    }
}



PreviousNext

Related