I'm working on a Spring Boot REST API that uses MySQL database. I'm trying to save a date (with time) received from client app "as is" which is in UTC time. Basically, it's already agreed upon that the dates will be sent back and forth in UTC and the conversion to appropriate timezone would be done on the client, so I already have UTC time in JSON and I'm trying to save it to a DATETIME column in MySQL.
I chose DATETIME because from MySQL reference it says that TIMESTAMP converts values, while DATETIME doesn't, which is exactly what I need.
I've created all the required entities to map my tables and again, from reference, I've seen that DATETIME maps to java.sql.Timestamp so that's what I used as type.
I'm using JpaRepository and calling it's save method to save my entity. What happens is: when I save dates to MySQL they get converted to UTC (I assume, because I'm in UTC+1 and the dates are one hour earlier than what I inserted).
I've read several answers on SO and I've tried using the following attributes in my jdbc connection string (as suggested here): noDatetimeStringSync=true
, useLegacyDatetimeCode=false
, sessionVariables=time_zone='-00:00'
but none of them worked.
I don't know if maybe spring does something that's messing with this?
This is part of the JSON I receive in request:
{
"dateFrom": "2019-12-01 16:00:00",
"dateTo": "2019-12-01 16:30:00"
}
My entity has these properties:
import java.sql.Timestamp;
...
private Timestamp dateFrom;
private Timestamp dateTo;
Table columns in db:
date_from datetime
date_to datetime
The entity has the right values inside it when I convert String to Timestamp and invoke save() method. So something between JpaRepository and the db itself messes with the dates.
UPDATE: The Timestamp actually wasn't the right value, it added ZoneInfo with ID="Europe/Prague"
and zoneOffset=3600000
while converting from String.
What I end up with is 2019-12-01 15:00:00
and 2019-12-01 15:30:00
in db.
What I would like is to store these dates exactly the way I received them. I even thought about switching to VARCHAR because I got so frustrated, but I don't want to do that because I need to perform queries based on dates etc.
Is there any way I can achieve what I'm looking for? It seemed pretty straight-forward at first, but now it's literally driving me insane.
[Additional info if required] I'm using:
- MySQL 5.7.27
- Spring Boot (starter-parent) 2.2.0.RELEASE
- mysql-connector-java
EDIT:
I might be going about this the wrong way, so if you could suggest how I could do this differently that would be awesome. The point is: my app needs to work for users in different timezones and they need to communicate with each other through dates. So if one user in timezone Europe/Prague says "let's talk tomorrow at 5pm" to another user in America/Chicago, I need to store this information in a way that can be translated for user in America in their local time (but also for any other user in any other timezone). This is why I opted for storing dates in UTC and then converting them on the client side to the user's local time. But apparently I'm misunderstanding something about how it all works.