我有开始日期和结束日期.我需要在这两个日期之间每天迭代一次.
最好的方法是什么?
我只能建议:
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