简体   繁体   English

将SQL UTC时间戳转换为本地时区Java

[英]Converting SQL UTC timestamp to local timezone Java

I have the following code. 我有以下代码。 I have stored a utc time in my mySQL db and would like to convert it to local time when I pull it from the database. 我已经在我的SQL数据库中存储了一个utc时间,并且当我从数据库中提取它时想将其转换为本地时间。

        Instant startInstant = rs.getTimestamp(5).toInstant();
        Instant endInstant = rs.getTimestamp(6).toInstant();
        ZoneId z = ZoneId.of(TimeZone.getDefault().getID());

        ZonedDateTime start = startInstant.atZone(z);
        ZonedDateTime end = startInstant.atZone(z);


        System.out.println(start.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss VV")));
        System.out.println(end.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss VV")));

This is the output I get: 这是我得到的输出:

2017-11-01 01:40:00 America/Chicago
2017-11-01 01:40:00 America/Chicago

The date and time are the same as in my db: This is the field value in the table: 日期和时间与我的数据库中的相同:这是表中的字段值:

2017-11-01 01:40:00

The only thing that seems to have updated is just the zoneID to America/Chicago which is functionally useless for what I'm needing. 似乎已更新的唯一内容只是America / Chicago的zoneID,对于我需要的功能在功能上毫无用处。

Any pointers as to where I've gone wrong? 关于我哪里出了问题的任何指示?

You have not supplied enough information to give an exact Answer. 您没有提供足够的信息来给出确切的答案。 You did not explain exactly the data type of the column around your date-time value. 您没有确切说明日期时间值周围的列的数据类型。 I suspect you did not use the MySQL equivalent to the SQL standard type of TIMESTAMP WITH TIME ZONE . 我怀疑您没有使用与TIMESTAMP WITH TIME ZONE的SQL标准类型等效的MySQL。

I also wonder why your example of a stored value lacks any offset-from-UTC or time zone. 我也想知道为什么您的存储值示例缺少从UTC到时区的偏移量。 That is suspicious. 那是可疑的。

Lastly, you may be looking st your stored value by using an interactive SQL session with a default time zone bring dynamically applied to the value retrieved from the database before displaying in your session. 最后,您可能会通过使用具有默认时区的交互式SQL会话来查找存储的值,该会话将在显示在会话中之前动态应用于从数据库中检索到的值。 While well-intentioned, I consider this an anti-feature as it masks the true nature of the stored data. 尽管出于很好的意图,但我认为这是一种反功能,因为它掩盖了存储数据的真实性质。 This behavior may explain your confusion. 此行为可能解释您的困惑。 Try explicitly setting your session's default time zone to UTC. 尝试将会话的默认时区明确设置为UTC。

Other issues with your code… 您代码的其他问题…

Skip the use of java.sql.Timestamp altogether. 完全跳过对java.sql.Timestamp的使用。 If your JDBC driver supports JDBC 4.2 or later, you can work directly with the java.time types. 如果JDBC驱动程序支持JDBC 4.2或更高版本,则可以直接使用java.time类型。

Instant instant = myResultSet.getObject( … , Instant.class ) ;

Do not mix an old legacy class like TimeZone with the modern java.time classes. 不要将TimeZone类的旧式旧类与现代的java.time类混合使用。 The legacy classes are completely supplanted by java.time; 遗留类完全被java.time取代; good riddance. 甩掉包袱。

ZoneId z = ZoneId.systemDefault() ;

You are getting the same value that you have entered in a database. 您将获得与在数据库中输入的相同的值。 You have timestamp values in database, but how you can say that the values stored are in UTC (the values might be in UTC but not in UTC format, so by looking into the value, you can't say that the value is in UTC -> '2017-11-01 01:40:00'). 您在数据库中有时间戳记值,但是如何说存储的值是以UTC表示的(这些值可能是UTC而不是UTC格式,所以通过查看该值,您不能说该值是UTC的->'2017-11-01 01:40:00')。

If you want to fetch the value in the different time zone, you can also do like this : 如果要在其他时区获取值,也可以这样:

eg: SET time_zone ='+03:00'; 例如:SET time_zone ='+ 03:00'; SELECT value FROM table; 从表中选择值;

It will be providing you the value in specified time zone. 它将为您提供指定时区的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM