For a given Date object, capture it's value relevant to GMT timezone

主宰稳场 提交于 2021-02-05 10:38:29

问题


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

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.

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!

For Ex:

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);
    }
}

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.: My ultimate goal is to have a the GMT value in a java.sql.TimeStamp object.

How could this be achieved?

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());

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?


回答1:


java.time and JDBC 4.2

The answer is in @BasilBourque’s comment: use 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);

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.

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

Some related questions

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



回答2:


First of all Date class is part of the old outdated (no pun intended) infrastructure. If it is at all possible get rid of it and just use java.time package. But if you must work with Date then your problem with time zone is not a problem. your line System.out.println(gmtDate); only prints it with your local time zone, since the system assumes that it is the best option. But regardless of that Date holds a particular moment in time in milliseconds since January 1, 1970, 00:00:00 GMT. Class Date has methods compareTo(), after() and before() that allow you to compare 2 Dates. Also Date has method getTime() that returns you the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this date. So you can compare the long values. But again, The best option is to switch to java.time package and classes Instant and ZonedDateTime (and others) have methods compareTo(), isAfter() and isBefore().




回答3:


You misunderstand the semantics of date time classes. java.util.Date is a specific point in time, an instant, it has no time zone associated with it. However if you have a time zone you can ask the time zone for the time of that java.util.Date.

java.sql.TimeStamp is the equivalent of java.time.LocalDateTime. It is not an instant and has no time zone associated with it.



来源:https://stackoverflow.com/questions/57808424/for-a-given-date-object-capture-its-value-relevant-to-gmt-timezone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!