Reputation: 59
I got a list of dates as String for example date1->'11-11-2010' and date2->'12-01-2011' I want to print all the dates between these two dates.. I tried to work with cal.add() but am not able to set my date1 to my cal.. if i do so i get null p
Upvotes: 3
Views: 1863
Reputation: 2141
below code should do the trick for you.
String date1 = "11-11-2010";
String date2 = "12-01-2011";
SimpleDateFormat format = new SimpleDateFormat("MM-dd-yyyy");
Calendar calendar1 = Calendar.getInstance();
calendar1.setTime(format.parse(date1));
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(format.parse(date2));
Date currentDate = calendar1.getTime();
while(!currentDate.equals(cal2.getTime())){
calendar1.add(Calendar.DAY_OF_MONTH, 1);
currentDate = cal1.getTime();
System.out.println(currentDate);
}
Upvotes: 3