182

In my script I need to perform a set of actions through range of dates, given a start and end date.
Please provide me guidance to achieve this using Java.

for ( currentDate = starDate; currentDate < endDate; currentDate++) {

}

I know the above code is simply impossible, but I do it in order to show you what I'd like to achieve.

Cache Staheli
  • 3,510
  • 7
  • 32
  • 51

15 Answers15

243

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.

Felk
  • 7,720
  • 2
  • 35
  • 65
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 2
    To expand on the point about Joda Time: trying to correctly implement this yourself is harder than one might think because of corner cases around changes to and from summer time. – Raedwald May 28 '13 at 08:01
  • +1 for Joda, I hope someday it will reach it's land in the standard API. – gyorgyabraham Aug 28 '13 at 12:29
  • 4
    @gyabraham: JSR-310 is looking in a pretty good shape for Java 8. – Jon Skeet Aug 29 '13 at 05:42
  • 4
    Can confirm this exact same code will work using Java 8's java.time.LocalDate instead of Joda. – Molten Ice Mar 21 '16 at 13:05
  • 3
    The Joda-Time project is now in maintenance mode, and recommends migration to the java.time classes. As mentioned in comments, this Answer’s code works as-is in java.time, just change your `import` statements. – Basil Bourque Nov 27 '17 at 16:56
162

JodaTime is nice, however, for the sake of completeness and/or if you prefer API-provided facilities, here are the standard API approaches.

When starting off with java.util.Date instances like below:

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date startDate = formatter.parse("2010-12-20");
Date endDate = formatter.parse("2010-12-26");

Here's the legacy java.util.Calendar approach in case you aren't on Java8 yet:

Calendar start = Calendar.getInstance();
start.setTime(startDate);
Calendar end = Calendar.getInstance();
end.setTime(endDate);

for (Date date = start.getTime(); start.before(end); start.add(Calendar.DATE, 1), date = start.getTime()) {
    // Do your job here with `date`.
    System.out.println(date);
}

And here's Java8's java.time.LocalDate approach, basically exactly the JodaTime approach:

LocalDate start = startDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
LocalDate end = endDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

for (LocalDate date = start; date.isBefore(end); date = date.plusDays(1)) {
    // Do your job here with `date`.
    System.out.println(date);
}

If you'd like to iterate inclusive the end date, then use !start.after(end) and !date.isAfter(end) respectively.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
94

Java 8 style, using the java.time classes:

// Monday, February 29 is a leap day in 2016 (otherwise, February only has 28 days)
LocalDate start = LocalDate.parse("2016-02-28"),
          end   = LocalDate.parse("2016-03-02");

// 4 days between (end is inclusive in this example)
Stream.iterate(start, date -> date.plusDays(1))
        .limit(ChronoUnit.DAYS.between(start, end) + 1)
        .forEach(System.out::println);

Output:

2016-02-28
2016-02-29
2016-03-01
2016-03-02

Alternative:

LocalDate next = start.minusDays(1);
while ((next = next.plusDays(1)).isBefore(end.plusDays(1))) {
    System.out.println(next);
}

Java 9 added the datesUntil() method:

start.datesUntil(end.plusDays(1)).forEach(System.out::println);
Martin Andersson
  • 18,072
  • 9
  • 87
  • 115
  • 1
    can you put multiples values ? for example: only monday or thursday or both –  Dec 27 '17 at 14:51
  • I don't understand the question? Sounds like perhaps you're looking for [`Stream.filter`](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/stream/Stream.html#filter(java.util.function.Predicate)). – Martin Andersson Dec 16 '21 at 07:12
30

This is essentially the same answer BalusC gave, but a bit more readable with a while loop in place of a for loop:

Calendar start = Calendar.getInstance();
start.setTime(startDate);

Calendar end = Calendar.getInstance();
end.setTime(endDate);

while( !start.after(end)){
    Date targetDay = start.getTime();
    // Do Work Here

    start.add(Calendar.DATE, 1);
}
Chris M.
  • 1,731
  • 14
  • 15
  • 3
    This will not work if the logic involves "continue" statements, while the for loop version of BalusC works with continue statements. – Sanjiv Jivan Dec 29 '14 at 15:13
8

Apache Commons

    for (Date dateIter = fromDate; !dateIter.after(toDate); dateIter = DateUtils.addDays(dateIter, 1)) {
        // ...
    }
Mike
  • 20,010
  • 25
  • 97
  • 140
  • +1, IMHO, this is the cleanest one when you're working with old code. Just throw in an extra static import for `addDays(..)` and it gets even shorter. – Priidu Neemre Jul 22 '19 at 13:59
7

It’s built in: LocalDate.datesUntil() since Java 9

It seems that the answers until now have only been considering Java 8 and earlier. The Java 9+ way is:

    LocalDate startDate = LocalDate.of(2021, Month.JUNE, 29);
    LocalDate endDate = LocalDate.of(2021, Month.JULY, 3);
    
    startDate.datesUntil(endDate).forEach(System.out::println);

Output from this example is:

2021-06-29
2021-06-30
2021-07-01
2021-07-02

While the start date is inclusive, the end date is exclusive, as in your question the way I read it. If someone wants the end date to be included, it’s easy, just add a day to it:

    startDate.datesUntil(endDate.plusDays(1)).forEach(System.out::println);
2021-06-29
2021-06-30
2021-07-01
2021-07-02
2021-07-03

You can obviously iterate several years in this way, just as you can put a lengthier lambda where I just put the method reference System.out::println for demonstration.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
5
private static void iterateBetweenDates(Date startDate, Date endDate) {
    Calendar startCalender = Calendar.getInstance();
    startCalender.setTime(startDate);
    Calendar endCalendar = Calendar.getInstance();
    endCalendar.setTime(endDate);

    for(; startCalender.compareTo(endCalendar)<=0;
          startCalender.add(Calendar.DATE, 1)) {
        // write your main logic here
    }

}
Dr Ken Reid
  • 577
  • 4
  • 22
kushal agrawal
  • 167
  • 2
  • 5
5

We can migrate the logic to various methods foe Java 7, Java 8 and Java 9:

public static List<Date> getDatesRangeJava7(Date startDate, Date endDate) {
    List<Date> datesInRange = new ArrayList<>();
    Calendar startCalendar = new GregorianCalendar();
    startCalendar.setTime(startDate);
    Calendar endCalendar = new GregorianCalendar();
    endCalendar.setTime(endDate);
    while (startCalendar.before(endCalendar)) {
        Date result = startCalendar.getTime();
        datesInRange.add(result);
        startCalendar.add(Calendar.DATE, 1);
    }
    return datesInRange;
}

public static List<LocalDate> getDatesRangeJava8(LocalDate startDate, LocalDate endDate) {
    int numOfDays = (int) ChronoUnit.DAYS.between(startDate, endDate);
    return IntStream.range(0, numOfDays)
            .mapToObj(startDate::plusDays)
            .collect(Collectors.toList());
}

public static List<LocalDate> getDatesRangeJava9(LocalDate startDate, LocalDate endDate) {
    return startDate.datesUntil(endDate).collect(Collectors.toList());
}

Then we can invoke these methods as:

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date startDate = formatter.parse("2010-12-20");
Date endDate = formatter.parse("2010-12-26");
List<Date> dateRangeList = getDatesRangeJava7(startDate, endDate);
System.out.println(dateRangeList);

LocalDate startLocalDate = startDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
LocalDate endLocalDate = endDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
List<LocalDate> dateRangeList8 = getDatesRangeJava8(startLocalDate, endLocalDate);
System.out.println(dateRangeList8);
List<LocalDate> dateRangeList9 = getDatesRangeJava8(startLocalDate, endLocalDate);
System.out.println(dateRangeList9);

The output would be:

[Mon Dec 20 00:00:00 IST 2010, Tue Dec 21 00:00:00 IST 2010, Wed Dec 22 00:00:00 IST 2010, Thu Dec 23 00:00:00 IST 2010, Fri Dec 24 00:00:00 IST 2010, Sat Dec 25 00:00:00 IST 2010]

[2010-12-20, 2010-12-21, 2010-12-22, 2010-12-23, 2010-12-24, 2010-12-25]

[2010-12-20, 2010-12-21, 2010-12-22, 2010-12-23, 2010-12-24, 2010-12-25]

akhil_mittal
  • 23,309
  • 7
  • 96
  • 95
  • 2
    The terrible `Date` and `Calendar` classes were supplanted by the *java.time* classes years ago. Specifically, replaced by `Instant` and `ZonedDateDate`. – Basil Bourque Aug 21 '18 at 06:04
  • 3
    I like the Java 8 and 9 ways. For Java 6 and 7 I recommend using [the ThreeTen Backport library](https://www.threeten.org/threetenbp/) and then the same way as in Java 8. You demonstrate nicely how this way is clearer and more programmer-friendly. – Ole V.V. Aug 21 '18 at 09:24
3
public static final void generateRange(final Date dateFrom, final Date dateTo)
{
    final Calendar current = Calendar.getInstance();
    current.setTime(dateFrom);

    while (!current.getTime().after(dateTo))
    {
        // TODO

        current.add(Calendar.DATE, 1);
    }
}
kayz1
  • 7,260
  • 3
  • 53
  • 56
2

Here is Java 8 code. I think this code will solve your problem.Happy Coding

    LocalDate start = LocalDate.now();
    LocalDate end = LocalDate.of(2016, 9, 1);//JAVA 9 release date
    Long duration = start.until(end, ChronoUnit.DAYS);
    System.out.println(duration);
     // Do Any stuff Here there after
    IntStream.iterate(0, i -> i + 1)
             .limit(duration)
             .forEach((i) -> {});
     //old way of iteration
    for (int i = 0; i < duration; i++)
     System.out.print("" + i);// Do Any stuff Here
1

Why not use epoch and loop through easily.

long startDateEpoch = new java.text.SimpleDateFormat("dd/MM/yyyy").parse(startDate).getTime() / 1000;

    long endDateEpoch = new java.text.SimpleDateFormat("dd/MM/yyyy").parse(endDate).getTime() / 1000;


    long i;
    for(i=startDateEpoch ; i<=endDateEpoch; i+=86400){

        System.out.println(i);

    }
mridul4c
  • 8,197
  • 3
  • 19
  • 28
1

You can write a class like it(implementing iterator interface) and iterate over it .

public class DateIterator implements Iterator<Date>, Iterable<Date>
{

 private Calendar end = Calendar.getInstance();
 private Calendar current = Calendar.getInstance();

 public DateIterator(Date start, Date end)
 {
     this.end.setTime(end);
     this.end.add(Calendar.DATE, -1);
     this.current.setTime(start);
     this.current.add(Calendar.DATE, -1);
 }

 @Override
 public boolean hasNext()
 {
     return !current.after(end);
 }

 @Override
 public Date next()
 {
     current.add(Calendar.DATE, 1);
     return current.getTime();
 }

 @Override
 public void remove()
 {
     throw new UnsupportedOperationException(
        "Cannot remove");
 }

 @Override
 public Iterator<Date> iterator()
 {
     return this;
 }
}

and use it like :

Iterator<Date> dateIterator = new DateIterator(startDate, endDate);
while(dateIterator.hasNext()){
      Date selectedDate = dateIterator .next();

}
jdev
  • 5,304
  • 1
  • 17
  • 20
1

You can try this:

OffsetDateTime currentDateTime = OffsetDateTime.now();
for (OffsetDateTime date = currentDateTime; date.isAfter(currentDateTime.minusYears(YEARS)); date = date.minusWeeks(1))
{
    ...
}
Dino
  • 7,779
  • 12
  • 46
  • 85
0

This will help you start 30 days back and loop through until today's date. you can easily change range of dates and direction.

private void iterateThroughDates() throws Exception {
    Calendar start = Calendar.getInstance();
    start.add(Calendar.DATE, -30);
    Calendar end = Calendar.getInstance();
    for (Calendar date = start; date.before(end); date.add(Calendar.DATE, 1))
        {
        System.out.println(date.getTime());
        }
}
Januka samaranyake
  • 2,385
  • 1
  • 28
  • 50
0

The following snippet (uses java.time.format of Java 8) maybe used to iterate over a date range :

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    // Any chosen date format maybe taken  
    LocalDate startDate = LocalDate.parse(startDateString,formatter);
    LocalDate endDate = LocalDate.parse(endDateString,formatter);
    if(endDate.isBefore(startDate))
    {
        //error
    }
    LocalDate itr = null;
    for (itr = startDate; itr.isBefore(endDate)||itr.isEqual(itr); itr = itr.plusDays(1))
    {
        //Processing  goes here
    }

The plusMonths()/plusYears() maybe chosen for time unit increment. Increment by a single day is being done in above illustration.

Devanshu Kashyap
  • 185
  • 1
  • 22