获取一年中每一天的所有完整时间
我需要在给定年份的每一天的每一小时都在命令行上获取/打印,例如2011 年,但我正在努力用 Java 编写代码.
I need to get / print on command line every full hour of every day of a given year, e.g. 2011 but I am struggling to code it in Java.
有人编码过这个问题吗?
Has anybody ever coded this issue?
推荐答案
这应该可行:
final DateFormat df = DateFormat.getDateTimeInstance();
final Calendar c = Calendar.getInstance();
c.clear();
for (c.set(2011, Calendar.JANUARY, 1, 0, 0, 0);
c.get(Calendar.YEAR) == 2011;
c.add(Calendar.HOUR_OF_DAY, 1))
System.out.println(df.format(c.getTime()));
例如,请注意输出中的这种微妙之处:
Notice, for example, this subtlety in the output:
Oct 30, 2011 12:00:00 AM
Oct 30, 2011 1:00:00 AM
Oct 30, 2011 2:00:00 AM
Oct 30, 2011 2:00:00 AM
Oct 30, 2011 3:00:00 AM
相关文章