gpt4 book ai didi

java - GregorianCalendar 意外值

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:16:49 30 4
gpt4 key购买 nike

TimeZone utcTimezone_ = TimeZone.getTimeZone("UTC");
TimeZone _utc = TimeZone.getTimeZone("Zulu");
DateFormat _fo = new SimpleDateFormat("yyyy-MM-dd HH:mm");
_fo.setTimeZone(_utc);
GregorianCalendar gc = new GregorianCalendar(_utc);

System.out.println(gc.getTime());
System.out.println(gc.get(GregorianCalendar.MINUTE));
System.out.println(gc.get(GregorianCalendar.HOUR_OF_DAY));

运行上述代码时的输出示例是:

Mon Sep 16 16:40:37 CST 2013
10
7

在这种情况下,我期望分钟为 40(不是 10),小时为 16(不是 7)。我现在对这一切有什么不理解的?谢谢。

编辑:如果我将时区更改为我本地时区的 CST,分钟和小时仍然不匹配:

TimeZone _utc = TimeZone.getTimeZone("CST");
DateFormat _fo = new SimpleDateFormat("yyyy-MM-dd HH:mm");
_fo.setTimeZone(_utc);
GregorianCalendar gc = new GregorianCalendar(_utc);

System.out.println(gc.getTime());
System.out.println(gc.get(GregorianCalendar.MINUTE));
System.out.println(gc.get(GregorianCalendar.HOUR_OF_DAY));

输出是:

Mon Sep 16 21:23:48 CST 2013
53
6

最佳答案

System.out.println(gc.getTime());

此行打印您计算机本地时区的日历(getTime() 返回一个 Date,其中包含与时区无关的纪元以来的毫秒数,然后隐式 toString() 调用使用本地时区打印它)。根据结果​​是CST。

System.out.println(gc.get(GregorianCalendar.MINUTE));
System.out.println(gc.get(GregorianCalendar.HOUR_OF_DAY));

这些行打印日历时区中的日历字段。根据您的代码,它是“祖鲁语”。

By the way, it is strongly recommended not to use three-letters timezones :

Three-letter time zone IDs

For compatibility with JDK 1.1.x, some other three-letter time zone IDs (such as "PST", "CTT", "AST") are also supported. However, their use is deprecated because the same abbreviation is often used for multiple time zones (for example, "CST" could be U.S. "Central Standard Time" and "China Standard Time"), and the Java platform can then only recognize one of them.

关于java - GregorianCalendar 意外值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18822327/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com