I am trying to retrieve a converted "hour" integer for a user's time zone with GMT unix time. My code works SOME of the time, though for example, it was 9:00pm on the east coast and getting a 0 for the hour. can anyone help?
我正在尝试检索具有GMT Unix时间的用户时区的转换后的“小时”整数。我的代码有时是有效的,但例如,现在是东海岸晚上9点,一小时得了0分。有谁能帮忙吗?
long l = Long.parseLong(oslist.get(position).get("hour"));
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(l);
calendar.setTimeInMillis(l * 1000);
calendar.setTimeZone(TimeZone.getDefault());
int hour = calendar.get(Calendar.HOUR);
Log.v("TIME:", ""+hour);
更多回答
You don't need to set the time zone - it's default by default, as it were. And calling setTimeInMillis
twice is pointless. So just:
您不需要设置时区--可以说,这是默认设置。并且两次调用setTimeInMillis是没有意义的。所以只要:
Calendar calendar = calendar.getInstance();
calendar.setTimeInMillis(unixTimestamp * 1000L);
int hour = calendar.get(Calendar.HOUR);
... should be absolutely fine. If it's not, then going via string representations as suggested by other answers isn't going to help.
..。应该完全没问题。如果不是,那么像其他答案所建议的那样通过字符串表示将不会有任何帮助。
If that's giving 0 when it's 9pm on the East Coast, that suggests the default time zone is not one representing the East Coast. I suggest you diagnose that first:
如果当东海岸是晚上9点的时候是0,那就表明默认时区不代表东海岸。我建议你先诊断一下:
System.out.println(TimeZone.getDefault().getID());
// Just in case the ID is misleading, what's the standard offset for this zone?
System.out.println(TimeZone.getDefault().getRawOffset());
java.time
The java.util
date-time API and their corresponding parsing/formatting type, SimpleDateFormat
are outdated and error-prone. In March 2014, the modern Date-Time API supplanted this API. Since then, it has been strongly recommended to switch to java.time
, the modern date-time API.
Util Date-Time API及其对应的解析/格式化类型SimpleDateFormat已经过时且容易出错。2014年3月,现代的Date-Time API取代了此API。从那时起,强烈建议改用现代的日期-时间API--java.time。
Solution using java.time
API:
使用java.time API的解决方案:
You can use Instant#ofEpochSecond
to get the Instant
corresponding to your given Unix timestamp. Next, convert the Instant
to a ZonedDateTime
at the required timezone and then you can get individual time units (e.g. hour) out of this ZonedDateTime
.
您可以使用Instant#of EpochSecond来获取与您给定的Unix时间戳对应的Instant。接下来,将Instant转换为所需时区的ZonedDateTime,然后您可以从该ZonedDateTime中获得单独的时间单位(例如小时)。
// e.g. a Unix timestamp representing 9:00pm on 10-Sep-2023 in New York
long epochSeconds = 1694394000L;
Instant instant = Instant.ofEpochSecond(epochSeconds);
ZonedDateTime zdt = instant.atZone(ZoneId.of("America/New_York"));
System.out.println(zdt);
System.out.println(zdt.getHour());
Output:
产出:
2023-09-10T21:00-04:00[America/New_York]
21
Online Demo
在线演示
Learn more about the modern date-time API from Trail: Date Time
从Trail:Date Time了解有关现代Date-Time API的更多信息
Also, if you need the timezone itself, see Class SimpleDateFormat. Specifically the z
and Z
formats.
此外,如果需要时区本身,请参见类SimpleDateFormat。具体地说,是z和Z格式。
Try this:
试试这个:
long l = Long.parseLong(oslist.get(position).get("hour"));
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(l);
calendar.setTimeInMillis(l * 1000);
Date date = calendar.getTime(); //current date and time in UTC
SimpleDateFormat df = new SimpleDateFormat("HH"); //HH = 0-23 hours
df.setTimeZone(TimeZone.getDefault()); //format in given timezone
String hourStringValue = df.format(date);
int hourIntValue = Integer.parseInt(hourStringValue);
更多回答
You don't need to use SimpleDateFormat
if the aim isn't to get a string representation. (And there's nothing in the OP's question suggesting they do want to convert to a string.)
如果目标不是获取字符串表示,则不需要使用SimpleDateFormat。(操作员的问题中没有任何内容表明他们确实想要转换为字符串。)
Formatting to a string and then parsing is a terrible way to approach this. Calendar
is fine.
将格式化为字符串,然后进行解析是一种糟糕的方法。日历没问题。
I did not know that Calendar by default convert the time to the current timezone. Thanks for the suggestion
我不知道日历默认情况下会将时间转换为当前时区。谢谢你的建议
我是一名优秀的程序员,十分优秀!