Java - 将java.time.Instant转换为没有Zone偏移的java.sql.Timestamp [英] Java - Convert java.time.Instant to java.sql.Timestamp without Zone offset

查看:131
本文介绍了Java - 将java.time.Instant转换为没有Zone偏移的java.sql.Timestamp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在开发的应用程序中,我需要将java.time.Instant对象转换为java.sql.Timestamp。当我创建Instant对象时,例如

In application I am developing I need to convert java.time.Instant object to java.sql.Timestamp. When I create Instant object, e.g

Instant now = Instant.now();

并检查元素。我收到类似 2017-03-13T14:28:59.970Z
然后我尝试创建这样的Timestamp对象:

and inspect the element. I receive something like 2017-03-13T14:28:59.970Z Then I try to create Timestamp object like this:

Timestamp current = Timestamp.from(now);

当我检查元素时。我收到的内容如 2017-03-13T16:28:59.970Z
相同的结果,但增加了2小时的偏移量。
任何人都可以解释你的这种情况并向我提供如何在没有抵消的情况下解决这个问题的答案吗?

And when I inspect the element. I receive something like 2017-03-13T16:28:59.970Z The same result but with added 2 hours offset. Can anyone explain thy this happens and provide me with answer how to solve this without offset ?.

当我这样创建时:

LocalDateTime ldt = LocalDateTime.ofInstant(Instnant.now(), ZoneOffset.UTC);
Timestamp current = Timestamp.valueOf(ldt);

一切正常。但我尽量避免转换。有没有办法只使用Instant对象?

Everything works fine. But I try to avoid conversions. Is there any way to do this only using Instant object ?

推荐答案

我将计算机的时区更改为欧洲/布加勒斯特进行实验。这是UTC + 2小时,就像你的时区一样。

I changed my computer’s time zone to Europe/Bucharest for an experiment. This is UTC + 2 hours like your time zone.

现在当我复制你的代码时,我得到的结果与你的相似:

Now when I copy your code I get a result similar to yours:

    Instant now = Instant.now();
    System.out.println(now); // prints 2017-03-14T06:16:32.621Z
    Timestamp current = Timestamp.from(now);
    System.out.println(current); // 2017-03-14 08:16:32.621

输出在评论中给出。但是,我继续:

Output is given in comments. However, I go on:

    DateFormat df = DateFormat.getDateTimeInstance();
    df.setTimeZone(TimeZone.getTimeZone("UTC"));
    // the following prints: Timestamp in UTC: 14-03-2017 06:16:32
    System.out.println("Timestamp in UTC: " + df.format(current));

现在你可以看到时间戳了同意我们开始的 Instant (只打印毫秒,但我相信它们也在那里)。所以你已经完成了所有事情并且只是混淆了因为当我们打印 Timestamp 时,我们隐式调用了它的 toString 方法,此方法依次抓取计算机的时区设置并显示该区域中的时间。只是因为这个,显示是不同的。

Now you can see that the Timestamp really agrees with the Instant we started out from (only the milliseconds are not printed, but I trust they are in there too). So you have done everything correctly and only got confused because when we printed the Timestamp we were implicitly calling its toString method, and this method in turn grabs the computer’s time zone setting and displays the time in this zone. Only because of this, the displays are different.

你尝试的另一件事,使用 LocalDateTime ,似乎工作,但它确实没有给你你想要的东西:

The other thing you attempted, using LocalDateTime, appears to work, but it really does not give you what you want:

    LocalDateTime ldt = LocalDateTime.ofInstant(Instant.now(), ZoneOffset.UTC);
    System.out.println(ldt); // 2017-03-14T06:16:32.819
    current = Timestamp.valueOf(ldt);
    System.out.println(current); // 2017-03-14 06:16:32.819
    System.out.println("Timestamp in UTC: " + df.format(current)); // 14-03-2017 04:16:32

现在打印时间戳使用我们的UTC DateFormat ,我们可以看到它是太早2小时,04:16:32 UTC 即时是06:16:32 UTC。所以这种方法是骗人的,看起来它正在工作,但事实并非如此。

Now when we print the Timestamp using our UTC DateFormat, we can see that it is 2 hours too early, 04:16:32 UTC when the Instant is 06:16:32 UTC. So this method is deceiving, it looks like it’s working, but it doesn’t.

这显示了导致设计Java 8日期和时间类的麻烦替换旧的。因此,您的问题的真正和良好的解决方案可能是让自己成为一个JDBC 4.2驱动程序,可以轻松接受 Instant 对象,这样您就可以避免转换为时间戳完全一样。我不知道你现在是否可以使用它,但我确信它会存在。

This shows the trouble that lead to the design of the Java 8 date and time classes to replace the old ones. So the real and good solution to your problem would probably be to get yourself a JDBC 4.2 driver that can accept an Instant object readily so you can avoid converting to Timestamp altogether. I don’t know if that’s available for you just yet, but I’m convinced it will be.

这篇关于Java - 将java.time.Instant转换为没有Zone偏移的java.sql.Timestamp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆