3

In a scala program, I receive from client side a specific date for instance:

2013-10-20T23:59:59.999Z

and I really want to keep this date when saving into DB and not convert to local, so this line:

debug("--sql timestamp: " + new Timestamp(reading.timestamp.getMillis()))

is printing out: 2013-10-21 02:59:59.999(I am in Romania).

Is there any way I can ignore timezone?

Cristian Boariu
  • 9,603
  • 14
  • 91
  • 162

2 Answers2

2

This is Timestamp.toString() behavior. java.sql.Timestamp extends java.util.Date and in its toString() method it uses, in particular, super.getHours(), which, according to javadoc, returns hours interpreted in local timezone - exactly as you observe.

However, internally Timestamp still holds correct timestamp value. There may be problems with storing it to the database, though. See this answer.

Community
  • 1
  • 1
Vladimir Matveev
  • 120,085
  • 34
  • 287
  • 296
2

2013-10-20T23:59:59.999Z and 2013-10-21 02:59:59.999 are actually the same time: 2013-10-20T23:59:59.999Z is in the UTC time zone (Z), whereas the second one is relative, and expressed as your local time zone (UTC+3 then in Romania).

In PostgreSQL, you should store your timestamps as TIMESTAMP WITH TIME ZONE (TIMESTAMPTZ) in your database to handle this. You'll always be able to print it out later in the time zone you choose then (e.g. UTC). (You might be interested in this recent question to understand why the storage type matters.)

If you want to print out the timestamp in the UTC/Z time zone again, new DateTime(millis, DateTimeZone.UTC) should help (with Joda Time).

Community
  • 1
  • 1
Bruno
  • 119,590
  • 31
  • 270
  • 376