Reputation: 837
Suppose I want Array Of Dates i want Dates between
Start Date- 26/9/2012 End Date-3/10/2012
diffinDays=7; What i am doing is
for(int i=0;i<diffInDays;i++){
DateArray[i]=StartDate.toString();
SimpleDateFormat sdf= new SimpleDateFormat("dd/mm/yyyy");
Calendar c= Calendar.getInstance();
try {
c.setTime(sdf.parse(StartDate));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
c.add(Calendar.DATE, 1);
// number of days to add
StartDate= sdf.format(c.getTime()); // dt is now the new date
}
It Addes me Date but when the Date is 30/09/2012 how can i add Month?
Upvotes: 0
Views: 758
Reputation: 339372
This is easier in Joda-Time.
DateTimeFormatter formatter = DateTimeFormat.forPattern( "d/M/yyyy" );
LocalDate start = formatter.parseLocalDate( "26/9/2012" );
// loop
LocalDate localDate = start.plusDays( i );
Upvotes: 0
Reputation: 21191
No need to add month when the date is 30/09/2012
. the calendar will automatically get the days how many you want. just mange the diffinDays
variable in for loop and you will see the array of dates in DateArray
. Just look at this code
String StartDate= "30/09/2012",EndDate="7/10/2012";
for(int i=0;i<diffinDays;i++){
DateArray[i]=StartDate.toString();
SimpleDateFormat sdf= new SimpleDateFormat("dd/mm/yyyy");
Calendar c= Calendar.getInstance();
try {
c.setTime(sdf.parse(StartDate));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
c.add(Calendar.DATE, 1);
// number of days to add
StartDate= sdf.format(c.getTime()); // dt is now the new date
System.out.println(DateArray[i]);
}
Upvotes: 1