I have a table which has field with data type as DATETIME. I persist a record in this table using java and I calculate current date as as following.
Date date = Calendar.getInstance(TimeZone.getTimeZone("UTC")).getTime();
But When I check my table, it again converts this time into local timezone.
Can some one please explain to me, how to store date in UTC timezone in database.
@Code
I have a db entity ‘Referral’ corresponding to table with date member data type as java.util.Date and using hibernate to save it.
Referral ref = new Referral();
ref.setCreationDate(Calendar.getInstance(TimeZone.getTimeZone("UTC")).getTime());
referralDAO.save(ref);
Thanks
Jitendra
MySql DATETIME is stored in a time zone agnostic manner. It just stores year/month/day/hour/minute/second
How you interpret that information is up to you and your application.
If you are storing it as UTC, then when you retrieve it you must make sure to interpret it as UTC as well.
You might be interested in this SO question: How can I get the current date and time in UTC or GMT in Java?
As mentioned in the answer to that question: