GregorianCalendar / 日历和设置 HOUR 字段的奇怪之处

发布于 2024-10-14 16:16:23 字数 1025 浏览 5 评论 0原文

我正在尝试创建一个“固定”时间(24 小时时钟格式的午夜,即 00:00:00)以使用以下命令设置为 SQL SELECT 查询的字符串...

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

GregorianCalendar todayDate = new GregorianCalendar();
Log.d(TAG, "todayDate: " + todayDate.getTime().toString());
Log.d(TAG, "formatted todayDate: " + sdf.format(todayDate.getTime()));
todayDate.clear(Calendar.HOUR);
todayDate.clear(Calendar.MINUTE);
todayDate.clear(Calendar.SECOND);
todayDate.set(Calendar.HOUR, 0);
todayDate.set(Calendar.MINUTE, 0);
todayDate.set(Calendar.SECOND, 0);
Log.d(TAG, "formatted modified todayDate: " + sdf.format(todayDate.getTime()));

这很好,除非当前时间是下午。例如,

todayDate: Fri Jan 28 23:34:34 GMT 2011
formatted todayDate: 2011-01-28 23:34:34
formatted modified todayDate: 2011-01-28 12:00:00 <- THE hour is 12 not 00

如果我在当前时间介于午夜和中午之间(即 00:00:00 -> 11:59:59 AM)时执行此操作,那么格式化字符串中的小时将正确设置为 00。如果我在中午之后和午夜之前的任何时间执行此操作,然后我的小时数为 12,而不是 00。

任何人都可以解释一下这一点并帮助我找到修复方法(或替代的做事方式)吗?

I'm trying to create a 'fixed' time (midnight in 24 hour clock format, i.e., 00:00:00) to set as a string for a SQL SELECT query using the the following...

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

GregorianCalendar todayDate = new GregorianCalendar();
Log.d(TAG, "todayDate: " + todayDate.getTime().toString());
Log.d(TAG, "formatted todayDate: " + sdf.format(todayDate.getTime()));
todayDate.clear(Calendar.HOUR);
todayDate.clear(Calendar.MINUTE);
todayDate.clear(Calendar.SECOND);
todayDate.set(Calendar.HOUR, 0);
todayDate.set(Calendar.MINUTE, 0);
todayDate.set(Calendar.SECOND, 0);
Log.d(TAG, "formatted modified todayDate: " + sdf.format(todayDate.getTime()));

This is fine UNLESS the current time is PM. For example,

todayDate: Fri Jan 28 23:34:34 GMT 2011
formatted todayDate: 2011-01-28 23:34:34
formatted modified todayDate: 2011-01-28 12:00:00 <- THE hour is 12 not 00

If I do this when the current time is between midnight and midday, (i.e., 00:00:00 -> 11:59:59 AM) then my hour in the formatted string is correctly set to 00. If I do it at any time after midday and before midnight then I get 12 for my hour and not 00.

Can anybody explain this and help me find a fix (or alternative way of doing things) please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温柔女人霸气范 2024-10-21 16:16:23

您需要将 HOUR_OF_DAY 设置为 0 而不是 HOUR

todayDate.set(Calendar.HOUR_OF_DAY, 0);

从 API 文档

HOUR get 和 set 的字段编号,指示上午或下午的时间。 HOUR 用于 12 小时制 (0 - 11)。中午和午夜用 0 表示,而不是 12。例如,在 10:04:15.250 PM,HOUR 是 10。

HOUR_OF_DAY 用于获取和设置的字段编号,指示一天中的小时。 HOUR_OF_DAY 用于 24 小时制。例如,晚上 10:04:15.250 HOUR_OF_DAY 为 22 点。

You need to set HOUR_OF_DAY to 0 instead of HOUR

todayDate.set(Calendar.HOUR_OF_DAY, 0);

From the API docs:

HOUR Field number for get and set indicating the hour of the morning or afternoon. HOUR is used for the 12-hour clock (0 - 11). Noon and midnight are represented by 0, not by 12. E.g., at 10:04:15.250 PM the HOUR is 10.

HOUR_OF_DAY Field number for get and set indicating the hour of the day. HOUR_OF_DAY is used for the 24-hour clock. E.g., at 10:04:15.250 PM the HOUR_OF_DAY is 22.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文