对于给定的Date对象,捕获与GMT时区相关的值 [英] For a given Date object, capture it's value relevant to GMT timezone

查看:76
本文介绍了对于给定的Date对象,捕获与GMT时区相关的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在运行于Java 8上的应用程序中,我有一个Date对象.该对象的时区取决于客户端的位置.

In my application running on Java 8, I have a Date object. The timezone for this object depends on the client's location.

在某个特定的时刻,我需要将此日期转换为GMT,以便它可以与我从数据库访问的值相当.

At one particular point, I need to convert this Date to GMT so that it could be comparable to a value that I access from the DB.

我尝试了SimpleDateFormat和ZonedDateTime,但是有一个痛点.这些API为我提供了String值的GMT时间,这非常好.但是,一旦我将其解析并分配给Date对象,它就会回到我的本地时区!

I tried SimpleDateFormat and ZonedDateTime, but there's a pain-point. These API's provide me GMT time in String values, which is perfectly fine. However, once I parse it and assign it to a Date object, it is back to my local timezone!

例如:

public class TimeZoneDemo {
    public static void main(String[] args) throws ParseException {
        Date istDate = Calendar.getInstance().getTime();

        ZonedDateTime gmtTime = istDate.toInstant().atZone(ZoneId.of("GMT"));
        System.out.println(gmtTime);

        Date gmtDate = Date.from(gmtTime.toInstant());
        System.out.println(gmtDate);
    }
}

在上面的代码中, gmtTime 显示与GMT时区相关的正确值,但 gmtDate (日期对象)在当地时区打印值.

In the code above, gmtTime displays the right value relevant to the GMT timezone, but gmtDate (the Date object) prints value in the local time zone.

P.S .:我的最终目标是在java.sql.TimeStamp对象中具有GMT值.

P.S.: My ultimate goal is to have a the GMT value in a java.sql.TimeStamp object.

如何实现?

更新1: 经过评论&答复我了解到,Date对象仅包含一个包含毫秒的长值. 但是,我的期望是在执行此行时:

UPDATE 1: After going through the comments & replies I understand that the Date object just holds a long value containing milliseconds. BUT, my expectation here is that when this line is executed:

Date gmtDate = Date.from(gmtTime.toInstant());

无论对象 gmtTime 包含什么时间,我都需要在TimeStamp或Date对象中捕获该时间.这样做的目的是能够与数据库中保留的值进行比较.我将日期作为参数传递给SQL查询.

Whatever time the object gmtTime contains, I need to capture just that time in a TimeStamp or Date object. The motive is to then be able to make comparison with a value persisted in the database. I'm passing the date as parameter to my SQL query.

有人可以帮助我了解如何实现这一目标吗?

Can someone pls help me understand how can this be achieved?

推荐答案

java.time和JDBC 4.2

答案在@BasilBourque的评论中:使用OffsetDateTime.

    PreparedStatement yourPreparedStatement = yourDatabaseConnection.prepareStatement(
            "select smth from your_table where your_time_stamp_col < ?;");
    OffsetDateTime gmtTime = OffsetDateTime.now(ZoneOffset.UTC);
    yourPreparedStatement.setObject(1, gmtTime);

这需要兼容JDBC 4.2的JDBC驱动程序,我认为到目前为止我们所有人都在使用该驱动程序.这很好,因为它允许我们绕过java.sql.Timestampjava.sql中的其他日期时间类型.它们的设计都差强人意,而且早已过时.

This requires a JDBC 4.2 compliant JDBC driver, I think that about all of us are using that by now. Which is good because it allows us to bypass java.sql.Timestamp and the other date-time types in java.sql. They are all poorly designed and long outdated.

正如其他人所说,过时的类DateTimestamp都没有任何时区或与UTC/GMT的时间偏移.

As others have said, neither of the outdated classes Date and Timestamp have any time zone or offset from UTC/GMT.

  • Get GMT Time in Java
  • SimpleDateFormat returns wrong time zone during parse
  • Getting the date from a ResultSet for use with java.time classes

这篇关于对于给定的Date对象,捕获与GMT时区相关的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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