public Calendar getDate(String dateString){
int year = Integer.parseInt(dateString.substring(0,dateString.indexOf("-")));
int mounth = Integer.parseInt(dateString.substring(dateString.indexOf("-")+1,dateString.lastIndexOf("-")));
int day = Integer.parseInt(dateString.substring(dateString.lastIndexOf("-")+1,dateString.lastIndexOf(" ")));
int hour = Integer.parseInt(dateString.substring(dateString.lastIndexOf(" ")+1,dateString.indexOf(":")));
int minutes = Integer.parseInt(dateString.substring(dateString.indexOf(":")+1,dateString.lastIndexOf(":")));
int second = Integer.parseInt(dateString.substring(dateString.lastIndexOf(":")+1));
Calendar date = Calendar.getInstance();
date.set(year, mounth-1, day ,hour,minutes,second);
return date;
}
Calendar beginDate = getDate("2014-12-31 12:00:00");
Calendar endDate = getDate("2015-1-31 12:00:00");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
while (beginDate.compareTo(endDate) <= 0) {
String currentString = sdf.format(beginDate.getTime());
sysout(currentString);
beginDate.set(Calendar.DATE, beginDate.get(Calendar.DATE) + 1);
}
如未加特殊说明,此网站文章均为原创,转载必须注明出处。
HollisChuang's Blog »
时间的遍历
现在java.time包处理这种问题比较方便了