转载

java – 我需要一个迭代日期间隔的循环

我有开始日期和结束日期.我需要在这两个日期之间每天迭代一次.

最好的方法是什么?

我只能建议:

Date currentDate = new Date (startDate.getTime ());
while (true) {
   if (currentDate.getTime () >= endDate.getTime ())
      break;
   doSmth ();
   currentDate = new Date (currentDate.getTime () + MILLIS_PER_DAY);
}
准备好运行;-)
public static void main(String[] args) throws ParseException {
    GregorianCalendar gcal = new GregorianCalendar();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
    Date start = sdf.parse("2010.01.01");
    Date end = sdf.parse("2010.01.14");
    gcal.setTime(start);
    while (gcal.getTime().before(end)) {
        gcal.add(Calendar.DAY_OF_YEAR, 1);
        System.out.println( gcal.getTime().toString());
    }
}

翻译自:https://stackoverflow.com/questions/2074197/i-need-a-cycle-which-iterates-through-dates-interval

原文  https://codeday.me/bug/20190111/516344.html
正文到此结束
Loading...