可能重复:
如何在 Java 中将日期增加一天?
public static ArrayList<Calendar> getCalendarsForThreeYears() {
if (calendars == null) {
calendars = new ArrayList<Calendar>();
final Calendar moving = Calendar.getInstance();
final int targetYear = moving.get(Calendar.YEAR) + 1;
moving.set(moving.get(Calendar.YEAR) - 1, Calendar.JANUARY, 1);
final SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yy");
while (!(moving.get(Calendar.YEAR) == targetYear
&& moving.get(Calendar.MONTH) == Calendar.DECEMBER && moving
.get(Calendar.DAY_OF_MONTH) == 31)) {
final Calendar c = (Calendar) moving.clone();
calendars.add(c);
moving.roll(Calendar.DAY_OF_MONTH, 1);
System.out.println(sdf.format(c.getTime()));
}
}
return calendars;
}
我希望它可以创建3年的Calendar
对象(当前之前的那个,当前的和下一个)。
然而,这是一个无限循环,因为Calendar.DAY_OF_MONTH
在月底滚动不会滚动Calendar.MONTH
推荐的方法是什么?