How to Iterate Through Range of Dates in Java

How to iterate through range of Dates in Java?

Well, you could do something like this using Java 8's time-API, for this problem specifically java.time.LocalDate (or the equivalent Joda Time classes for Java 7 and older)

for (LocalDate date = startDate; date.isBefore(endDate); date = date.plusDays(1))
{
...
}

I would thoroughly recommend using java.time (or Joda Time) over the built-in Date/Calendar classes.

How To iterate the Start Date to end Date in java

You can use Java Calendar to achieve this :

Date start = new Date();
Date end = new Date();

Calendar cStart = Calendar.getInstance(); cStart.setTime(start);
Calendar cEnd = Calendar.getInstance(); cEnd.setTime(end);

while (cStart.before(cEnd)) {

//add one day to date
cStart.add(Calendar.DAY_OF_MONTH, 1);

//do something...
}

Iterate through date range Java, strange results

Month that the library return start at 0 .. 11

Also your loop could be simplified liked this. Note that this might not be the best solution.

while(!start.after(end)) 
{
int year = start.get(Calendar.YEAR);
int month = start.get(Calendar.MONTH) + 1;
int day = start.get(Calendar.DAY_OF_MONTH);
System.out.printf("%d.%d.%d\n", day, month, year);
start.add(Calendar.DATE, 1);
}

Iterate between two date range using Joda

Actually there's a simple way around to your original code you posted, see my implementation below, just modified your for loop implementation:

    //test data
LocalDate startDate = LocalDate.now(); //get current date
LocalDate endDate = startDate.plusDays(5); //add 5 days to current date

System.out.println("startDate : " + startDate);
System.out.println("endDate : " + endDate);

for(LocalDate currentdate = startDate;
currentdate.isBefore(endDate) || currentdate.isEqual(endDate);
currentdate= currentdate.plusDays(1)){
System.out.println(currentdate);
}

Below is the Output (with respect to my localDate):

startDate : 2015-03-26

endDate : 2015-03-31

2015-03-26

2015-03-27

2015-03-28

2015-03-29

2015-03-30

2015-03-31

Hope this helps! Cheers. :)

Iterate over dates range (the scala way)

You may use plusDays:

val now = DateTime.now
(0 until 5).map(now.plusDays(_)).foreach(println)

Given start and end dates:

import org.joda.time.Days

val start = DateTime.now.minusDays(5)
val end = DateTime.now.plusDays(5)

val daysCount = Days.daysBetween(start, end).getDays()
(0 until daysCount).map(start.plusDays(_)).foreach(println)

Iterate between two dates including start date?

In my opinion this is the nicest way:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM");
Date start = sdf.parse("2010.01");
Date end = sdf.parse("2010.04");

GregorianCalendar gcal = new GregorianCalendar();
gcal.setTime(start);

while (!gcal.getTime().after(end)) {
Date d = gcal.getTime();
System.out.println(d);
gcal.add(Calendar.MONTH, 1);
}

Output:

Fri Jan 01 00:00:00 WST 2010
Mon Feb 01 00:00:00 WST 2010
Mon Mar 01 00:00:00 WST 2010
Thu Apr 01 00:00:00 WST 2010

All we do is print the date before incrementing it, then we repeat if the date is not after the end date.

The other option is to duplicate the printing code before the while (yuck) or to use a do...while (also yuck).



Related Topics



Leave a reply



Submit