gpt4 book ai didi

java - 将时区设置为已经在 UTC 中的 UTC

转载 作者:行者123 更新时间:2023-11-29 14:31:46 32 4
gpt4 key购买 nike

我在应用程序中将时区设置为 UTC,如下所示:

// set default time zone to UTC
TimeZone.setDefault(TimeZone.getTimeZone(ZoneOffset.UTC));

这适用于我们以 UTC 格式存储在数据库中的所有日期字段。但是一个日期已经以 UTC 格式出现,因此以上时区将再次将其设置为 UTC 格式。

如何避免在 UTC 中再次设置它?

一些代码-

public static void main(String[] args) throws DatatypeConfigurationException {
DatatypeFactory datatypeFactory = DatatypeFactory.
newInstance();
// set default time zone to UTC
TimeZone.setDefault(TimeZone.getTimeZone(ZoneOffset.UTC));
// coming from external program in UTC but assigned it here for reference
long someTimeToBeInUTC = 1528371000000L;
GregorianCalendar start = new GregorianCalendar();
start.setTimeInMillis(someTimeToBeInUTC);
XMLGregorianCalendar startXmlCalendar =
datatypeFactory.newXMLGregorianCalendar(start);
System.out.println(startXmlCalendar.
toGregorianCalendar().getTime());

}

想想我有一个时刻 someTimeToBeInUTC 而不管时区,然后因为行 TimeZone.setDefault(TimeZone.getTimeZone(ZoneOffset.UTC)) 它设置它在 UTC。

然后在 hibernate 状态下,我在存储此值的表的 hbm.xml 中拥有属性-

<property name="startTime" type="timestamp">
<column name="start_time" length="29" />
</property>

在postgres中startTime的类型是timestamp without timezone。因此,当我们保存时,它会再次将其转换为 UTC。

最佳答案

以毫秒为单位的时间与时区无关。

无论您在 Calendar、Date 或 Timestamp 对象中的何处使用它,以毫秒为单位的时间都是准确的时间点。

请仔细阅读 Java 文档。它始终表示从纪元 (1/1/1970 00:00:00 UTC) 开始经过的毫秒数。

时区仅在使用这些对象的其他表示时才有效。例如从 toString() 方法,从 Calendar 等获取字段)

更新假设您有以毫秒为单位的时间 1528371250000L...

然后你可以有两个日历(它们是时区感知的)一个在 UTC 一个在东部

查看完整的代码示例,以说明在同时设置以毫秒为单位的时间时发生了什么:

public static void main(String... args) throws Exception {

Calendar utcCalendar = Calendar
.getInstance(TimeZone.getTimeZone("UTC"));

utcCalendar.setTimeInMillis(1528371250000L);

System.out.println("UTC Calendar: \t\t" + (utcCalendar.get(Calendar.MONTH) +1)
+ "/" + utcCalendar.get(Calendar.DAY_OF_MONTH) + "/"
+ utcCalendar.get(Calendar.YEAR) + " "
+ utcCalendar.get(Calendar.HOUR_OF_DAY) + ":"
+ utcCalendar.get(Calendar.MINUTE) + ":"
+ utcCalendar.get(Calendar.SECOND) + " " + utcCalendar.getTimeZone().getID());

Calendar eastCalendar = Calendar.getInstance(TimeZone
.getTimeZone("EST"));

eastCalendar.setTimeInMillis(1528371250000L);

System.out.println("Eastern Calendar: \t"
+ (eastCalendar.get(Calendar.MONTH)+1) + "/"
+ eastCalendar.get(Calendar.DAY_OF_MONTH) + "/"
+ eastCalendar.get(Calendar.YEAR) + " "
+ eastCalendar.get(Calendar.HOUR_OF_DAY) + ":"
+ eastCalendar.get(Calendar.MINUTE) + ":"
+ eastCalendar.get(Calendar.SECOND) + " " + eastCalendar.getTimeZone().getID());

XMLGregorianCalendar xmlUtcCalendar = DatatypeFactory.newInstance()
.newXMLGregorianCalendar((GregorianCalendar) utcCalendar);

System.out.println("XML UTC Calendar: \t" + xmlUtcCalendar.toString());

XMLGregorianCalendar xmlEastCalendar = DatatypeFactory.newInstance()
.newXMLGregorianCalendar((GregorianCalendar) eastCalendar);

System.out.println("XML Eastern Calendar: \t" + xmlEastCalendar.toString());

}

输出:

 UTC Calendar:          6/7/2018 11:34:10 UTC
Eastern Calendar: 6/7/2018 6:34:10 EST
XML UTC Calendar: 2018-06-07T11:34:10.000Z
XML Eastern Calendar: 2018-06-07T06:34:10.000-05:00

关于java - 将时区设置为已经在 UTC 中的 UTC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50699661/

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