public static void main(String[] args) {
//起始时间
Calendar begindate = new GregorianCalendar();
//截至时间
Calendar enddate = new GregorianCalendar();
//"2010-05-19 23:59:59"~"2010-05-20 23:59:59"
begindate.set(2010, 4, 19, 23, 59, 59);
enddate.set(2010, 4, 20, 23, 59, 59);
enddate.add(Calendar.SECOND, 1);
while (begindate.before(enddate)) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(begindate.getTime()));
//每次加一秒
begindate.add(Calendar.SECOND, 1);
}
}
输出内容
2010-05-19 23:59:59
2010-05-20 00:00:00
2010-05-20 00:00:01
2010-05-20 00:00:02
2010-05-20 00:00:03
2010-05-20 00:00:04
2010-05-20 00:00:05
2010-05-20 00:00:06
2010-05-20 00:00:07
2010-05-20 00:00:08
2010-05-20 00:00:09
2010-05-20 00:00:10
2010-05-20 00:00:11
2010-05-20 00:00:12
2010-05-20 00:00:13
2010-05-20 00:00:14
......
本文提供了一个使用Java进行日期时间遍历的具体示例,通过设置起始时间和结束时间,并利用Calendar类和SimpleDateFormat实现从一天的结束到另一天结束时间的逐秒输出。
2073

被折叠的 条评论
为什么被折叠?



