1


We have running application built on JAVA and J2EE product and has postgres as the database server, during design architect made a mistake and stored all date and time columns in local timezone which is IST. Now users of application are demanding to show all time in local zone. User from London wants application to show all date and time in GMT.
Is there any solution or approach which would best fit in here with minimum changes. Also i think use of Java time or JODA time will not help here because of more work to me.
1) Was thinking to convert all existing date and time to UTC ( easy, doable and better to implement)
2) How will i handle display part because in search screens we allow users to select not only date but time to find all matching transactions and time here is suppose to be local time of user.

5
  • What data type are you using in the database? timestamp without time zone? timestamp with time zone?
    – assylias
    Mar 2, 2016 at 11:32
  • we are using timestamp with time zone.
    – Amarjeet
    Mar 2, 2016 at 12:09
  • Well then it shouldn't matter - assuming you have a timestamp such as 02-March-2016 17:51 IST in your DB, using JDBC, you will get a Java Timestamp object which has no time zone information. You can then create a Calendar (if you really want to stick to the legacy api - which I don't recommend) with whatever timezone you like, for example 2016-03-02T12:21Z...
    – assylias
    Mar 2, 2016 at 12:17
  • Got your point but it will solve the issue for display that too only for one zone user, what if i get user from different timezone? In system we also allow users to search transaction based on date and time how will system handle that.
    – Amarjeet
    Mar 3, 2016 at 5:20
  • I don't understand what you are saying - you can use whatever time zone you want in your java code, regardless of the time zone used in the database. I suggest you show an example with the date stored in the DB and the code you use to retrieve it and show it.
    – assylias
    Mar 3, 2016 at 9:27

1 Answer 1

0

Background

In Postgres both TIMESTAMP and TIMESTAMP WITH TIMEZONE are stored the same way - number of seconds since Postgres epoch (2000-01-01). The main difference is what Postgres do when it saves timestamp value such as 2004-10-19 10:23:54+02:

  • without TZ the +02 is just stripped away
  • with TZ a -02 correction is performed to make it UTC

Now the interesting thing is when JDBC driver loads the value:

  • without TZ the stored value is shifted by the user's (JVM / OS) TZ
  • with TZ the value is considered to be UTC

In both the cases you will end up with java.sql.Timestamp object with user's default TZ.

Time Zones

Timestamps without TZ are pretty limited. If you have two systems connected to your database, both with different TZ, they will interpret timestamps differently.

You can tell JDBC what kind of TZ it should use when reading timestamp via ResultSet#getTimestamp(String, Calendar). Excerpt from JavaDoc:

This method uses the given calendar to construct an appropriate millisecond value for the timestamp if the underlying database does not store timezone information.

Once you have the timestamp (in your case without TZ), you can now reliably use Joda-Time. Joda-Time provide various built-in formatters, and you may also define your own. But for writing logs or reports, the best choice may be ISO 8601 format. That format happens to be the default used by Joda-Time and java.time.

Example Code

java.sql.Timestamp timestamp = resultSet.getTimestamp(i);

DateTime dateTimeUtc = new DateTime( DateTimeZone.UTC ); // Defaults to now, this moment.

// Convert as needed for presentation to user in local time zone.
DateTimeZone timeZone = DateTimeZone.forID("Europe/Paris");
DateTime dateTimeZoned = dateTimeUtc.toDateTime( timeZone );
1
  • Dear DROY, referring to your above line "In both the cases you will end up with java.sql.Timestamp object with user's default TZ." Here system is hosted in IST and default TZ will always be IST (ASIA/KOLKATTA). But we have application users from different timezone and they can initiated transaction. As of now since everything is IST users are not able to search their transactions based on their time, they do manual calculation and than search. The above solution will not help to tackle described problem.
    – Amarjeet
    Mar 3, 2016 at 5:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.