0

I have installed Cassandra 2.2.5 on a Linux m/c with JDK 1.8.

Now I am using CassandraTemplate provided in spring-cassandra-data jar to insert records in the Cassandra DB.

However the timestamp is getting converted into GMT i.e. 5:30 hours is deducted from it before it gets stored in the DB.

Ex: TimeStamp passed is '2016-02-29 13:25:21'
and TimeStamp stored is 2016-02-29 07:55:21+0000

I want the actual timestamp to be stored in the DB.

Thanks in advance.

scott_lotus
  • 3,171
  • 22
  • 51
  • 69
Sunil
  • 364
  • 1
  • 14

1 Answers1

1

As you already noticed, all dates will be converted to GMT (or UTC to be precise) before getting stored as timestamp values (64bit long) in the DB. This will be handled automatically for you by the Cassandra java driver.

Retrieving the stored date and converting it back to a specific timezone works too. Calling getTimestamp on the resultset will instantiate a java.util.Date object using the long value read from the result. You can afterwards convert the date to any timezone using java.util.Calendar or java.time.

Stefan Podkowinski
  • 5,206
  • 1
  • 20
  • 25
  • Ok, Can't we change the default TimeZone of Cassandra – Sunil Feb 29 '16 at 11:30
  • You need to change the timezone of your client. Changing the default on the JVM can be done by setting the system property: `-Duser.timezone=America/Chicago` – Stefan Podkowinski Feb 29 '16 at 12:03
  • Tried this but still the date stored in DB is in GMT TimeZone. Date as seen in Tomcat logger is _Tue Mar 01 18:39:16 IST 2016_ and date stored in DB is _2016-03-01 13:09:16+0000_ Also I have tried the steps mentioned in [http://stackoverflow.com/questions/32274369/how-to-convert-zoneddatetime-to-date] but this is also storing the date in GMT – Sunil Mar 01 '16 at 13:17
  • 2
    The date is always converted to GMT when saved to Cassandra. You're probably better of saving it as a string value if you need to preserve the timezone, or use timestamp + TZ-string value combination. – Stefan Podkowinski Mar 01 '16 at 13:55
  • i know this is a old post but still you can refer my solution [here](http://stackoverflow.com/questions/40986740/cassandra-returns-variable-results-when-looking-for-time-series-data/40988950?noredirect=1#comment69262990_40988950) – deenbandhu Dec 08 '16 at 07:17