java HOUR 和 HOUR_OF_DAY 均返回 12 小时时间
我正在使用以下代码尝试获取 unix 时间戳的 HOUR_OF_DAY (0-23),并将其转换为毫秒。时间戳“1296442971”转换为 Sun Jan 30 2011 22:02:51 GMT-0500 (EST)。
我正在运行以下代码来尝试获取 24 小时时间戳:
//calculate the hour for this timestamp
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getTimeZone(tz));
calendar.setTimeInMillis(ts * 1000);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int twelveHour = calendar.get(Calendar.HOUR);
在此示例中,“hour”和“twelveHour”的值为 10,而“hour”的值为“22”。有人对我的代码可能有什么问题有任何想法吗?
谢谢!
I am using the following code to try to get the HOUR_OF_DAY (0-23) of a unix timestamp, converted to milliseconds. The timestamp '1296442971' converts to Sun Jan 30 2011 22:02:51 GMT-0500 (EST).
I'm running the following code to try to get the 24-hr timestamp:
//calculate the hour for this timestamp
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getTimeZone(tz));
calendar.setTimeInMillis(ts * 1000);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int twelveHour = calendar.get(Calendar.HOUR);
In this example, both 'hour' and 'twelveHour' have the value 10, when 'hour' should have the value '22'. Does anyone have any ideas as to what could be wrong with my code?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
假设ts是包含值1296442971的变量。我相信您没有将其声明为long类型,因此可能会溢出 将
ts更改为long类型后,下面的工作
Assuming ts is the variable containing the value 1296442971. I believe you have not declared it to be of type long and hence it might be overflowing
Below works after changing ts to long type
这分别为我打印 22 和 10。这对于纽约来说看起来是正确的(因为你提到了东部时间)。
This prints 22 and 10 for me respectively. That looks correct for NY (since you mentioned eastern time).
您的问题几乎可以肯定是您正在使用
int
来保存秒数。输入以下检查int
和long
: 的程序,您将得到:
Your problem is almost certainly that you're using an
int
to hold the seconds. Type in the following program which checksint
andlong
:and you get:
太长了;博士
…for value:
2011-01-30T23:02:51-04:00[America/New_York]
32 位与 64 位整数
正如其他答案正确指出的那样,您必须使用 64 -bit
long
或Long
跟踪 UTC 自 1970 年以来的毫秒数。您的数字1296442971
是自 1970 年以来 UTC 的整秒数。当乘以 1,000 来获取自纪元以来的毫秒数时,就会溢出 32 位int
或Integer
的限制。java.time
另一个问题是您正在使用麻烦的旧遗留日期时间类,现在已被 java.time 类。
即时
Instant
类表示 UTC 中时间轴上的时刻,带有分辨率为纳秒。Instant
类有一个工厂方法,用于导入自纪元以来的整秒数。ZonedDateTime
要查看美国东海岸的挂钟时间,请为时区应用
ZoneId
例如America/New_York
来获取ZonedDateTime
对象。通过调用
getHour
。关于 java.time
java.time< /a> 框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧日期时间类,例如 java.util.Date、.Calendar 和 & 。
java.text.SimpleDateFormat
。Joda-Time 项目,现已在 维护模式,建议迁移到 java.time。
要了解更多信息,请参阅 Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。
许多 java.time 功能都向后移植到 Java 6 和 Java 6。 ThreeTen-Backport 中的 7 并进一步适应 Android 在 ThreeTenABP(请参阅如何使用...)。
ThreeTen-Extra 项目通过附加类扩展了 java.time。该项目是 java.time 未来可能添加的内容的试验场。您可能会在这里找到一些有用的类,例如
Interval
、YearWeek
、YearQuarter
等。tl;dr
…for value:
2011-01-30T23:02:51-04:00[America/New_York]
32-bit vs 64-bit integers
As the other answers correctly noted, you must use a 64-bit
long
orLong
to track the number of milliseconds since 1970 in UTC. Your number1296442971
is the number of whole seconds since 1970 in UTC. When multiplied by 1,000 to get milliseconds-since-epoch, you overflow the limit of a 32-bitint
orInteger
.java.time
Another problem is that you are using troublesome old legacy date-time classes, now supplanted by the java.time classes.
Instant
The
Instant
class represents a moment on the timeline in UTC with a resolution of nanoseconds.The
Instant
class has a factory method for importing a number of whole seconds since epoch.ZonedDateTime
To see the wall-clock time of the east coast US, apply a
ZoneId
for a time zone such asAmerica/New_York
to get aZonedDateTime
object.Interrogate for the hour-of-day in 24-hour numbering of 0-23 by calling
getHour
.About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old date-time classes such as
java.util.Date
,.Calendar
, &java.text.SimpleDateFormat
.The Joda-Time project, now in maintenance mode, advises migration to java.time.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.
Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP (see How to use…).
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as
Interval
,YearWeek
,YearQuarter
, and more.