Hi I need to find the In between Dates for java the example for
StartDate=2017-01-28
and
EndDate=2017-02-0
3
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..
Hi I need to find the In between Dates for java the example for
StartDate=2017-01-28
and
EndDate=2017-02-0
3
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..
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...
}
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();
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.