Home How to store Timestamp in MS SQL Server with JPA?
Reply: 0

How to store Timestamp in MS SQL Server with JPA?

user6417
1#
user6417 Published in September 20, 2018, 12:44 pm

I have an application that works with JPA and stores date and time using UTC. I am adding support for MS SQL Server and not sure how to store time. The standard datetime/datetime2 returns data in local timezone only and not UTC.

I considered using datetimeoffset SQL Server data type, but instead of the standart java.sql.Timestamp it returns a proprietary object, incompatible with other databases I am working with.

The only semi-working solution that I found is to force JVM timezone to be UTC (e.g. TimeZone.setDefault(TimeZone.getTimeZone("UTC"))). It causes MS SQL Server driver to return time in UTC for the datetime/datetime2 data type.

Is there a better a solution?

share|improve this question
  • 1
    See also stackoverflow.com/questions/14070572/… – Mark Rotteveel Feb 12 at 15:02
  • @MarkRotteveel I am using JPA AttributeConverter<Instant, Timestamp>, so, unfortunately, this answer does not help. I saw that there is a Calendar object inside java.sql.Timestamp, but it is inaccessible without resorting to reflection. – Sergey Feb 13 at 8:00

1 Answer 1

active oldest votes
up vote 0 down vote
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.394653 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO