KUSHAL
KUSHAL

Reputation: 63

To get start date and end date of months between the given dates range

i want to make a list of dates between the given date range like--

ex: fromDate - 15/10/2013 and toDate - 15/12/2013

now the list should be

[ 15/10/2013, 31/10/2013, 1/1/2013, 30/11/2013, 1/12/2013, 15/12/2013 ]

i have written this code ---

private List<Date> datesList = new ArrayList<Date>();

public void findLastDate(){
Date lastDateOfMonth;
Date firstDateOfNextMonth = null;

datesList.add(fromDate);

do {

Calendar calendar = Calendar.getInstance();  
if(firstDateOfNextMonth == null){
    calendar.setTime(fromDate);  
} else {
        calendar.setTime(firstDateOfNextMonth);  
}

calendar.add(Calendar.MONTH, 1);  
calendar.set(Calendar.DAY_OF_MONTH, 1);  
calendar.add(Calendar.DATE, -1);  

    lastDateOfMonth = calendar.getTime();  

    datesList.add(lastDateOfMonth);

    Calendar calendar2 = Calendar.getInstance();  
    calendar2.setTime(lastDateOfMonth);  

    calendar2.add(Calendar.MONTH, 1);  
    calendar2.set(Calendar.DAY_OF_MONTH, 1);  
    calendar2.add(Calendar.DATE, 0);  

    firstDateOfNextMonth = calendar2.getTime();  

    datesList.add(firstDateOfNextMonth);

} while(firstDateOfNextMonth.compareTo(toDate) < 0);

datesList.add(toDate);

    System.out.println("List of dates " + datesList);

}

but i am getting a list as

[15/10/2013, 31/10/2013, 1/1/2013, 30/11/2013, 1/12/2013, 30/12/2013, 1/1/2014, 15/12/2013]

the dates 30/12/2013, 1/1/2014 should not be in the list so what should be the condition in while loop i am not getting it plz help me out...

Upvotes: 1

Views: 1828

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347332

Basically, you should be doing 2 things.

  1. After you add a value to the Calendar, you need to be checking to see if that is now after the end time and
  2. Unless it's equal to the end time, removing the last date from the list

For Example

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

Calendar cal = Calendar.getInstance();
cal.set(Calendar.DATE, 15);
cal.set(Calendar.MONTH, 9);
cal.set(Calendar.YEAR, 2013);

Calendar endCal = Calendar.getInstance();
endCal.set(Calendar.DATE, 15);
endCal.set(Calendar.MONTH, 11);
endCal.set(Calendar.YEAR, 2013);

dates.add(cal.getTime());
while (cal.before(endCal)) {

    cal.set(Calendar.DATE, cal.getActualMaximum(Calendar.DATE));
    dates.add(cal.getTime());
    if (cal.before(endCal)) {
        cal.add(Calendar.DATE, 1);
        dates.add(cal.getTime());
    }

}

dates.remove(dates.size() - 1);
dates.add(endCal.getTime());
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
for (Date d : dates) {
    System.out.println(sdf.format(d));
}

Will output

15/10/2013
31/10/2013
01/11/2013
30/11/2013
01/12/2013
15/12/2013

Updated with example, based on original code

Calendar cal = Calendar.getInstance();
cal.set(Calendar.DATE, 15);
cal.set(Calendar.MONTH, 9);
cal.set(Calendar.YEAR, 2013);
Date fromDate = cal.getTime();

Calendar endCal = Calendar.getInstance();
endCal.set(Calendar.DATE, 15);
endCal.set(Calendar.MONTH, 11);
endCal.set(Calendar.YEAR, 2013);
Date toDate = endCal.getTime();

List<Date> datesList = new ArrayList<>(25);
datesList.add(fromDate);

Calendar calendar = Calendar.getInstance();
calendar.setTime(fromDate);
do {

    calendar.add(Calendar.MONTH, 1);
    calendar.set(Calendar.DAY_OF_MONTH, 1);
    calendar.add(Calendar.DATE, -1);

    if (calendar.getTime().before(toDate)) {

        datesList.add(calendar.getTime());

        calendar.add(Calendar.MONTH, 1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.DATE, 0);

        if (calendar.getTime().before(toDate)) {

            datesList.add(calendar.getTime());

        }

    }

} while (calendar.getTime().before(toDate));

datesList.add(toDate);

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
for (Date d : datesList) {
    System.out.println(sdf.format(d));
}

Which outputs...

15/10/2013
31/10/2013
01/11/2013
30/11/2013
01/12/2013
15/12/2013

Note: The one thing that neither of these code snippets are doing is check to see if the last date in the list is actually the end date (although from the looks of it, yours seems to be able to get around this need)

Upvotes: 1

Related Questions