获取一年中每一天的所有完整时间 [英] Get all full hours of every day of a year

查看:21
本文介绍了获取一年中每一天的所有完整时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在给定年份的每一天的每一小时都在命令行上获取/打印,例如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

这篇关于获取一年中每一天的所有完整时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆