Java – increment the date by 1 and cycle to the end of the month

I have a string date & I think through 1& it should loop to the end of the month

String date="12/01/2010";
String incDate;
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Calendar c = Calendar.getInstance();
c.setTime(sdf.parse(date));
for(int co=0; co<30; co++){
    c.add(Calendar.DATE,1); 
    incDate = sdf.format(c.getTime());
}

Solution

String date="12/01/2010";
String date="12/01/2010";
String incDate;
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Calendar c = Calendar.getInstance();
c.setTime(sdf.parse(date));
int maxDay = c.getActualMaximum(Calendar.DAY_OF_MONTH);
for(int co=0; co<maxDay; co++){
    c.add(Calendar.DATE,1); 
    incDate = sdf.format(c.getTime());
}

c. The getactualmaximum (calendar. Day_of_month) result will be the last day of the month

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>