java HOUR 和 HOUR_OF_DAY 均返回 12 小时时间

发布于 2024-10-15 04:52:32 字数 567 浏览 2 评论 0原文

我正在使用以下代码尝试获取 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 技术交流群。

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

发布评论

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

评论(4

坐在坟头思考人生 2024-10-22 04:52:32

假设ts是包含值1296442971的变量。我相信您没有将其声明为long类型,因此可能会溢出 将

ts更改为long类型后,下面的工作

long l = 1296442971;
calendar.setTimeInMillis(l * 1000);
out.println(calendar.getTime());
out.println(calendar.get(Calendar.HOUR_OF_DAY));
out.println(calendar.get(Calendar.HOUR));

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

long l = 1296442971;
calendar.setTimeInMillis(l * 1000);
out.println(calendar.getTime());
out.println(calendar.get(Calendar.HOUR_OF_DAY));
out.println(calendar.get(Calendar.HOUR));
阿楠 2024-10-22 04:52:32

这分别为我打印 22 和 10。这对于纽约来说看起来是正确的(因为你提到了东部时间)。

    long ts = 1296442971;
    Calendar calendar = Calendar.getInstance();       
    TimeZone tz = TimeZone.getTimeZone("America/New_York");
    calendar.setTimeZone(tz);
    calendar.setTimeInMillis(ts*1000);
    int hour = calendar.get(Calendar.HOUR_OF_DAY);  
    int twelveHour = calendar.get(Calendar.HOUR);
    System.out.println("hour:"+hour);
    System.out.println("twelvehour:"+twelveHour);

This prints 22 and 10 for me respectively. That looks correct for NY (since you mentioned eastern time).

    long ts = 1296442971;
    Calendar calendar = Calendar.getInstance();       
    TimeZone tz = TimeZone.getTimeZone("America/New_York");
    calendar.setTimeZone(tz);
    calendar.setTimeInMillis(ts*1000);
    int hour = calendar.get(Calendar.HOUR_OF_DAY);  
    int twelveHour = calendar.get(Calendar.HOUR);
    System.out.println("hour:"+hour);
    System.out.println("twelvehour:"+twelveHour);
醉态萌生 2024-10-22 04:52:32

您的问题几乎可以肯定是您正在使用 int 来保存秒数。输入以下检查 intlong: 的程序,

import java.util.Calendar;
import java.util.TimeZone;

public class scratch {
    public static void main (String args[]) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeZone(TimeZone.getTimeZone("America/New_York"));

        int intval = 1296442971;
        calendar.setTimeInMillis(intval * 1000);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);  
        int twelveHour = calendar.get(Calendar.HOUR);
        System.out.println (hour);
        System.out.println (twelveHour);

        long longval = 1296442971L;
        calendar.setTimeInMillis(longval * 1000);
        hour = calendar.get(Calendar.HOUR_OF_DAY);  
        twelveHour = calendar.get(Calendar.HOUR);
        System.out.println (hour);
        System.out.println (twelveHour);
    }
}

您将得到:

10
10
22
10

Your problem is almost certainly that you're using an int to hold the seconds. Type in the following program which checks int and long:

import java.util.Calendar;
import java.util.TimeZone;

public class scratch {
    public static void main (String args[]) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeZone(TimeZone.getTimeZone("America/New_York"));

        int intval = 1296442971;
        calendar.setTimeInMillis(intval * 1000);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);  
        int twelveHour = calendar.get(Calendar.HOUR);
        System.out.println (hour);
        System.out.println (twelveHour);

        long longval = 1296442971L;
        calendar.setTimeInMillis(longval * 1000);
        hour = calendar.get(Calendar.HOUR_OF_DAY);  
        twelveHour = calendar.get(Calendar.HOUR);
        System.out.println (hour);
        System.out.println (twelveHour);
    }
}

and you get:

10
10
22
10
单身狗的梦 2024-10-22 04:52:32

太长了;博士

Instant.ofEpochSecond( 1_296_442_971L )
       .atZone( ZoneId.of( "America/New_York" ) )
       .getHour()

23

…for value: 2011-01-30T23:02:51-04:00[America/New_York]

32 位与 64 位整数

正如其他答案正确指出的那样,您必须使用 64 -bit longLong 跟踪 UTC 自 1970 年以来的毫秒数。您的数字 1296442971 是自 1970 年以来 UTC 的整秒数。当乘以 1,000 来获取自纪元以来的毫秒数时,就会溢出 32 位 intInteger 的限制。

java.time

另一个问题是您正在使用麻烦的旧遗留日期时间类,现在已被 java.time 类。

即时

Instant 类表示 UTC 中时间轴上的时刻,带有分辨率为纳秒

Instant 类有一个工厂方法,用于导入自纪元以来的整秒数。

Instant instant = Instant.ofEpochSecond( 1_296_442_971L );

instant.toString(): 2011-01-31T03:02:51Z

ZonedDateTime

要查看美国东海岸的挂钟时间,请为时区应用 ZoneId例如 America/New_York 来获取 ZonedDateTime 对象。

ZoneId z = ZoneId.of( "America/New_York" );
ZonedDateTime zdt = instant.atZone( z );

zdt.toString(): 2011-01-30T23:02:51-04:00[美国/纽约]

通过调用 getHour

int hourOfDay = zdt.getHour();

23

关于 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 并进一步适应 AndroidThreeTenABP(请参阅如何使用...)。

ThreeTen-Extra 项目通过附加类扩展了 java.time。该项目是 java.time 未来可能添加的内容的试验场。您可能会在这里找到一些有用的类,例如 IntervalYearWeekYearQuarter 等。

tl;dr

Instant.ofEpochSecond( 1_296_442_971L )
       .atZone( ZoneId.of( "America/New_York" ) )
       .getHour()

23

…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 or Long to track the number of milliseconds since 1970 in UTC. Your number 1296442971 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-bit int or Integer.

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.

Instant instant = Instant.ofEpochSecond( 1_296_442_971L );

instant.toString(): 2011-01-31T03:02:51Z

ZonedDateTime

To see the wall-clock time of the east coast US, apply a ZoneId for a time zone such as America/New_York to get a ZonedDateTime object.

ZoneId z = ZoneId.of( "America/New_York" );
ZonedDateTime zdt = instant.atZone( z );

zdt.toString(): 2011-01-30T23:02:51-04:00[America/New_York]

Interrogate for the hour-of-day in 24-hour numbering of 0-23 by calling getHour.

int hourOfDay = zdt.getHour();

23

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.

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