Date d = Calendar.getTime() 返回错误的时间?
Calendar calendar = Calendar.getInstance();
calendar.set(calendar.HOUR, 8);
calendar.set(calendar.MINUTE, 45);
calendar.set(calendar.SECOND, 00);
Date d = calendar.getTime();
System.out.println(d);
输出是: 2011 年 10 月 5 日星期三 20:45:00 BST
任何人都可以帮我解释为什么会这样吗?
Calendar calendar = Calendar.getInstance();
calendar.set(calendar.HOUR, 8);
calendar.set(calendar.MINUTE, 45);
calendar.set(calendar.SECOND, 00);
Date d = calendar.getTime();
System.out.println(d);
Output is:
Wed Oct 05 20:45:00 BST 2011
Can anyone help me with why this is?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Calendar.HOUR
< /a> 用于设置 12 小时制的小时。换句话说,您确实将
日历
设置为20:45。现在是晚上 8:45。使用
Calendar.HOUR_OF_DAY
设置 24 小时制值。Calendar.HOUR
is used to set the hour as set on a 12-hour clock.In other words, you do set the
Calendar
to 20:45. It's 8:45 PM.Use
Calendar.HOUR_OF_DAY
to set the 24-hour-clock value.当然 - 您正在设置
日历。 HOUR
代表 1-12“半天的小时”。您应该使用HOUR_OF_DAY
这是全天的 0-23 值:或者,使用 Joda Time是更好的日期/时间 API :)
顺便说一句,请不要通过引用引用静态成员...当您调用
someOtherThread.sleep(.. .)
并且你当前的线程处于休眠状态...Sure - you're setting
Calendar.HOUR
which represents the 1-12 "hour of half day". You should be usingHOUR_OF_DAY
which is the 0-23 value for the whole day:Alternatively, use Joda Time which is a much nicer date/time API :)
As an aside, please don't refer to static members via references... it will lead to pain when you call
someOtherThread.sleep(...)
and your current thread sleeps...java.time
java.util
Date-Time API 及其格式化 APISimpleDateFormat
已过时且容易出错。建议完全停止使用它们并切换到 现代日期时间 API*。使用现代日期时间 API
java.time
的解决方案:< kbd>在线演示
Instant
表示 UTC。输出中的Z
是时区指示符零时区偏移。它代表 Zulu,指定Etc/UTC
时区(时区偏移量为+00:00
小时)。无论出于何种原因,如果您需要将
Instant
对象转换为java.util.Date
对象,可以按如下方式进行:注意:< /strong> 最有可能的是,您也希望秒的分数为零。如果是,只需在现有代码中添加
.withNano(0)
即可,如下所示:从 跟踪:日期时间。
* 无论出于何种原因,如果您必须坚持使用 Java 6 或 Java 7,您可以使用 ThreeTen-Backport 将大部分 java.time 功能向后移植到 Java 6 和 Java 6 7. 如果您正在处理 Android 项目,并且您的 Android API 级别仍然不符合 Java-8,请检查 通过脱糖提供 Java 8+ API 和 如何在Android项目中使用ThreeTenABP。
java.time
The
java.util
Date-Time API and their formatting API,SimpleDateFormat
are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.Solution using
java.time
, the modern Date-Time API:ONLINE DEMO
An
Instant
represents an instantaneous point on the timeline in UTC. TheZ
in the output is the timezone designator for a zero-timezone offset. It stands for Zulu and specifies theEtc/UTC
timezone (which has the timezone offset of+00:00
hours).For any reason, if you need to convert this object of
Instant
to an object ofjava.util.Date
, you can do so as follows:Note: Most likely, you would like the fraction-of-second too to be zero. If yes, just add
.withNano(0)
in the existing code as shown below:Learn more about the modern Date-Time API from Trail: Date Time.
* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.