增加日期1&循环到月底 [英] Increment date by 1 & loop until end of the month

查看:98
本文介绍了增加日期1&循环到月底的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

i hav String date&我想通过1&应该循环到月底。
作为例如,如果我采取2010年11月应该循环30天。
如果我采取2010年12月应该循环31天。
以下显示我的代码......

i hav String date & i want to inceament date by 1 & it should be loop until end of the month. as examle, if i take November 2010 it should loop 30 days. if i take December 2010 it should loop 31 days. below shows my code......

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());
}


推荐答案

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.getActualMaximum(Calendar.DAY_OF_MONTH) / code>结果将是本月的最后一天。

The c.getActualMaximum(Calendar.DAY_OF_MONTH) result will be the last day of the month.

这篇关于增加日期1&amp;循环到月底的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆