Reputation: 2558
I am trying to get the list of dates between in a date range in iso format ("yyyy-MM-dd"), as follows:
ArrayList<String> datesList = new ArrayList<String>();
try {
SimpleDateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd");
int[] fromDateParts = new int[3];
for (int f = 0; f < fromIsoDate.split("-").length; f++) {
fromDateParts[f] = Integer.parseInt(fromIsoDate.split("-")[f]);
System.out.println("fromDateParts[" + f + "] : " + fromDateParts[f]);
}
int[] toDateParts = new int[3];
for (int t = 0; t < toIsoDate.split("-").length; t++) {
toDateParts[t] = Integer.parseInt(toIsoDate.split("-")[t]);
System.out.println("toDateParts[" + t + "] : " + toDateParts[t]);
}
Calendar fromDate = new GregorianCalendar(fromDateParts[0], fromDateParts[1], fromDateParts[2]);
Calendar toDate = new GregorianCalendar(toDateParts[0], toDateParts[1], toDateParts[2]);
System.out.println("fromDate " + fromDate + " toDate " + toDate);
boolean hasMoreDays = true;
while (hasMoreDays) {
if ((fromDate.before(toDate)) || (fromDate.equals(toDate))) {
datesList.add(isoFormat.format(fromDate));
fromDate.add(Calendar.DAY_OF_MONTH, 1);
} else {
hasMoreDays = false;
}
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("Exception : While determining the dates in between " + fromIsoDate + " and " + toIsoDate + "!");
}
But, it throws error:
java.lang.IllegalArgumentException: Cannot format given Object as a Date.
Please guide me in getting the list of dates between in a date range!
Upvotes: 1
Views: 721
Reputation: 425
You should pass a Date
to your SimpleDateFormat.fotmat
inntead of Calendar
:
datesList.add(isoFormat.format(fromDate));
to
datesList.add(isoFormat.format(fromDate.getTime));
And below is my solution for your request:
String fromStr = "2016-11-01";
String toStr = "2016-11-17";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar from = Calendar.getInstance();
from.setTime(sdf.parse(fromStr));
Calendar to = Calendar.getInstance();
to.setTime(sdf.parse(toStr));
List<Date> retval = new ArrayList<Date>();
while (from.before(to) || from.equals(to)) {
retval.add(from.getTime());
System.out.println(sdf.format(from.getTime()));
from.add(Calendar.DATE, 1);
}
Hope this help!
Upvotes: 1
Reputation: 5871
If you use java8 you can write the whole stuff in one line as shown below, I have used java.util.Date
List<Date> dates = new ArrayList<>();
Date fromDate = new Date(2016, 11, 02);
Date toDate = new Date(2016, 11, 28);
dates.add(new Date(2016, 11, 01));
dates.add(new Date(2016, 11, 02));
dates.add(new Date(2016, 11, 03));
dates.add(new Date(2016, 11, 04));
List<Date> filteredDates = dates.stream()
.filter((e) -> e.after(fromDate) && e.before(toDate)).collect(Collectors.toList());
Upvotes: 0
Reputation: 521103
Just wrap your start date in a Calendar
and use that:
Date startDate;
Date endDate;
Calendar c = Calendar.getInstance();
c.setTime(startDate);
Date dt = startDate;
List<Date> dates = new ArrayList<>();
do {
dates.add(dt);
c.add(Calendar.DATE, 1);
dt = c.getTime();
} while (endDate.getTime() > dt.getTime());
As you would expect to hear, the Java 8 API or possibly JodaTime might make your life easier than using the Java 7 date way of things.
Upvotes: 1