1

Hi I need to find the In between Dates for java the example for

StartDate=2017-01-28 and

EndDate=2017-02-03

I expect the output:

2017-01-28 
2017-01-29 
2017-01-30 
2017-01-31 
2017-02-01 
2017-02-02 
2017-02-03 

Please help me thank You..

Community
  • 1
  • 1
BalaMurugan.N
  • 120
  • 1
  • 1
  • 10

3 Answers3

6

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...
}
Mouad EL Fakir
  • 3,609
  • 2
  • 23
  • 37
3

Answer in Java 8 using package java.time.

StringBuilder builder = new StringBuilder();
LocalDate startDate = LocalDate.parse("2017-01-28");
LocalDate endDate = LocalDate.parse("2017-02-03");
LocalDate d = startDate;

while (d.isBefore(endDate) || d.equals(endDate)) {
  builder.append(d.format(DateTimeFormatter.ISO_DATE)).append(" ");
  d = d.plusDays(1);
}

// "2017-01-28 2017-01-29 2017-01-30 2017-01-31 2017-02-01 2017-02-02 2017-02-03"
String result = builder.toString().trim();
Mincong Huang
  • 5,284
  • 8
  • 39
  • 62
1

Well, you could do something like this (using Joda Time)

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

I would thoroughly recommend using Joda Time over the built-in Date/Calendar classes.

Sathiamoorthy
  • 8,831
  • 9
  • 65
  • 77