tags:

views:

585

answers:

5

I want to get a list of dates between start and end date.If i give the start and end date means, it give the result as the list of all dates including the start and end date.
Thanks in advance.

A: 

One solution would be to create a Calendar instance, and start a cycle, increasing it's Calendar.DATE field until it reaches the desired date. Also, on each step you should create a Date instance (with corresponding parameters), and put it to your list.

Some dirty code:

    public List<Date> getDatesBetween(final Date date1, final Date date2) {
    List<Date> dates = new ArrayList<Date>();

    Calendar calendar = new GregorianCalendar() {{
        set(Calendar.YEAR, date1.getYear());
        set(Calendar.MONTH, date1.getMonth());
        set(Calendar.DATE, date1.getDate());
    }};

    while (calendar.get(Calendar.YEAR) != date2.getYear() && calendar.get(Calendar.MONTH) != date2.getMonth() && calendar.get(Calendar.DATE) != date2.getDate()) {
        calendar.add(Calendar.DATE, 1);
        dates.add(new Date(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DATE)));
    }

    return dates;
}
folone
You do know that you use a deprecated API? Yeah., java date handling sucks.
extraneon
Yeah, that's why I wrote, that this code is dirty.
folone
A: 

You can also look at the Date.getTime() API. That gives a long to which you can add your increment. Then create a new Date.

List<Date> dates = new ArrayList<Date>();
long interval = 1000 * 60 * 60; // 1 hour in millis
long endtime = ; // create your endtime here, possibly using Calendar or Date
long curTime = startDate.getTime();
while (curTime <= endTime) {
  dates.add(new Date(curTime));
  curTime += interval;
}

and maybe apache commons has something like this in DateUtils, or perhaps they have a CalendarUtils too :)

EDIT

including the start and enddate may not be possible if your interval is not perfect :)

extraneon
+5  A: 

I suggest to use jodaTime for that

List<LocalDate> dates = new ArrayList<LocalDate>();
int days = Days.daysBetween(startDate, endDate).getDays();
for (int i=0; i < days; i++) {
    LocalDate d = startDate.withFieldAdded(DurationFieldType.days(), i);
    dates.add(d);
}

It wouldn't be too hard to implement your own iterator to do this aswell, that would be even nicer.

Albert
+1 definitely better than mine.
extraneon
jodaTime is awesome!
Ham
A: 

This one works better

 public static List<Date> obtenerFechasDiariasIntervalo(Date fechaInicial, Date fechaFinal)
{
    List<Date> dates = new ArrayList<Date>();
    Calendar calendar = new GregorianCalendar();
    calendar.setTime(fechaInicial);

    while (calendar.getTime().before(fechaFinal))
    {
        Date resultado = calendar.getTime();
        dates.add(resultado);
        calendar.add(Calendar.DATE, 1);
    }
    return dates;
}
Jules Rogerson
A: 

Hi,

please find the below code.

        List<Date> dates = new ArrayList<Date>();

        String str_date ="27/08/2010";
        String end_date ="02/09/2010";

        DateFormat formatter ; 

        formatter = new SimpleDateFormat("dd/MM/yyyy");
        Date  startDate = (Date)formatter.parse(str_date); 
        Date  endDate = (Date)formatter.parse(end_date);
        long interval = 24*1000 * 60 * 60; // 1 hour in millis
        long endTime =endDate.getTime() ; // create your endtime here, possibly using Calendar or Date
        long curTime = startDate.getTime();
        while (curTime <= endTime) {
          dates.add(new Date(curTime));
          curTime += interval;
        }
        for(int i=0;i<dates.size();i++){
            Date lDate =(Date)dates.get(i);
            String ds = formatter.format(lDate);

        System.out.println(" Date is ..." + ds);
        }

output:

Date is ...27/08/2010
Date is ...28/08/2010
Date is ...29/08/2010
Date is ...30/08/2010
Date is ...31/08/2010
Date is ...01/09/2010
Date is ...02/09/2010

bidyot
This wont work around the end of daylight saving time since that day has 25 hours. You will get the next day repeated. Example for Germany: Date is ...30/10/2010 | Date is ...31/10/2010 | Date is ...31/10/2010 | Date is ...01/11/2010 (Simple solution: add some hours (12) to the start date)
Carlos Heuberger