0

I am trying to get dates, which occurs in every 5 days from start date to end date.

Eg.
if start date= 11/10/2014   i.e  MM/DD/YYYY format
and  end date =11/26/2014

then my **expected output** is =
 [11/15/2014,11/20/2014,11/25/2014]

I tried below but very confuse where to run loop for getting exact ouput. Currently from below code i am only getting 1 date in list

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


public class TestDate {
        //     mm/dd/yyyy
    public List getDates(Date fromDate,int frequency,Date endDate){
        List list=new ArrayList<Date>();
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");


        Calendar c = Calendar.getInstance();
        c.setTime(fromDate); // Now use today date. 
        c.add(Calendar.DATE, frequency); // Adding 5 days  
        String newDate = sdf.format(c.getTime());
         String sEndDate=sdf.format(endDate);

        if((newDate.compareTo(sEndDate) < 0) || (newDate.compareTo(sEndDate) == 0)){
            list.add(newDate); 
        }

        //Weekly=7,Bi-Weekly14,Monthly-30,Semi-Monthly-15 
        return list; 
    }
    public static void main(String[] args) {
        try {
        TestDate obj=new TestDate();
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
        Date s = sdf.parse("11/10/2014");
        Date e = sdf.parse("11/26/2014");
        System.out.println(obj.getDates(s, 5, e));
        }
        catch(Exception e) { 
            System.err.println("--exp in main---"+e);   
        }
    }
}

Correct answer is below *Thanks to Almas*

public List getDates(Date fromDate,int frequency,Date e){
        List list=new ArrayList<Date>(); 
        Calendar c = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c2.setTime(e); // Now use today date.  
         Date endDate=c2.getTime();   
        Date newDate=fromDate;
        while(true){
            c.add(Calendar.DATE, frequency);
            newDate=c.getTime();

            if(newDate.compareTo(endDate)<=0){
                list.add(newDate);
            }else{
                break;
            }
        } 
        //Weekly=7,Bi-Weekly14,Monthly-30,Semi-Monthly-15 
        return list; 
    }
Dan
  • 2,086
  • 11
  • 71
  • 137
  • Is my question is clear? can you understand what i am asking? Because still there is no reply, so i am thinking no one is understanding my question. – Dan Nov 10 '14 at 10:55
  • You asked your question less than 5 minutes ago! – assylias Nov 10 '14 at 10:59
  • Lot of problems with your code : 1) `list` is initialized as an `ArrayList`, but you put `String` in it 2) when you are comparing `newDate` to `sEndDate` you are doing a `String` compare, not a `Date` compare – Florent Bayle Nov 10 '14 at 11:05
  • As a possible [example](http://stackoverflow.com/questions/16785643/get-the-list-of-dates-between-two-dates/16785762#16785762) – MadProgrammer Nov 10 '14 at 11:14
  • possible duplicate of [How can I increment a date by one day in Java?](http://stackoverflow.com/questions/428918/how-can-i-increment-a-date-by-one-day-in-java) – Basil Bourque Nov 10 '14 at 18:01

3 Answers3

1

Why do you want date to be as string and compared lexicographically instead of comparing them as date?

String newDate = sdf.format(c.getTime());
String sEndDate=sdf.format(endDate);

This should be changed as

 Date newDate = c.getTime();

Also you are using two if condition which you could do in one like below :

if (newDate.compareTo(endDate) <= 0) {
    list.add(newDate);
}

as far as looping is concerned you should do it in your getDates method like below:

Date newDate;
while (true) {
    c.add(Calendar.DATE, frequency); // Adding 5 days
    newDate = c.getTime();

    if (newDate.compareTo(endDate) <= 0) {
        list.add(newDate);
    } else {
        break;
    }
}    
SMA
  • 36,381
  • 8
  • 49
  • 73
  • i was making it in String because i need output in "MM/DD/YYYY" format – Dan Nov 10 '14 at 11:09
  • You could do it post getting in list or while adding to list as well. – SMA Nov 10 '14 at 11:12
  • and `endDate` date i will recive in this format "MM/DD/YYYY" only, so its string. so i was doing like that – Dan Nov 10 '14 at 11:13
  • That's ok please have a look and try it in your way (don't blindly copy paste this code) and see if that helps you to understand what was wrong and how you could have done better. – SMA Nov 10 '14 at 11:15
  • ok. I updated my code above but it is missing intial date. Output Should be `[Sat Nov 15 00:00:00 IST 2014, Thu Nov 20 00:00:00 IST 2014, Tue Nov 25 00:00:00 IST 2014]` but my new update code is giving only `[Thu Nov 20 00:00:00 IST 2014, Tue Nov 25 00:00:00 IST 2014]` why? – Dan Nov 10 '14 at 11:31
  • Compare the code again, as for me i personally ran the code and its giving output as expected. c'mon don't be lazy, try and debug ;-) – SMA Nov 10 '14 at 11:34
0

Make use of Apache commons DateUtils. This will make your code simple

    Date tempDate = DateUtils.addDays(fromDate, frequency);
    while (tempDate.before(endDate))
    {
        list.add(tempDate);
        tempDate = DateUtils.addDays(tempDate, frequency);
    }
    return list; 
Sangeeth
  • 614
  • 1
  • 5
  • 14
0

try this one

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

public class NewClass1 {
    //     mm/dd/yyyy

    public List getDates(Date fromDate, int frequency, Date endDate) {
        List list = new ArrayList<Date>();
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");


        Calendar c = Calendar.getInstance();
        c.setTime(fromDate); // Now use today date.
        c.add(Calendar.DATE, frequency); // Adding 5 days
        String newDate = sdf.format(c.getTime());
        //System.out.println("date"+newDate);
        String formDate = sdf.format(fromDate);
        String sEndDate = sdf.format(endDate);
        int x = 1;
        while (((newDate.compareTo(sEndDate) > 0) || (newDate.compareTo(sEndDate) != 0)) && x < frequency) {
            c.add(Calendar.DATE, frequency);
            sEndDate = sdf.format(c.getTime());
            x++;
            System.out.println("date: " + sEndDate);
            list.add(newDate);
        }

        //Weekly=7,Bi-Weekly14,Monthly-30,Semi-Monthly-15
        return list;
    }

    public static void main(String[] args) {
        try {
            NewClass1 obj = new NewClass1();
            SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
            Date s = sdf.parse("11/10/2014");
            Date e = sdf.parse("11/26/2014");
            obj.getDates(s, 5, e);
        } catch (Exception e) {
            System.err.println("--exp in main---" + e);
        }
    }
}
Chandan Sarma
  • 308
  • 2
  • 5
  • 19