gpt4 book ai didi

java - Java 中的日历不适用于 UTC 时区

转载 作者:行者123 更新时间:2023-12-01 08:52:01 35 4
gpt4 key购买 nike

我的代码:

TimeZone timeZone = TimeZone.getTimeZone("UTC");
Calendar cal = (Calendar) Calendar.getInstance(timeZone);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EE MMM dd HH:mm:ss zzz yyyy", Locale.US);
System.out.println("Gregorian cal time:"+cal.getTime());
System.out.println("utc time: "+simpleDateFormat.format(cal.getTime()));

两个打印语句都显示相同的本地时间。

最佳答案

您需要将 DateFormatter 的时区设置为“UTC”

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EE MMM dd HH:mm:ss zzz yyyy", Locale.US);
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println("utc time: "+simpleDateFormat.format(cal.getTime()));

关于java - Java 中的日历不适用于 UTC 时区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42328060/

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