A user picks a date and a time on a page in my web app (Grails, if that matters). I know the user's time zone (they set it in user preferences). I now want to save that Date to the database as UTC (using Hibernate, if that matters).
So, here is my domain class:
class Even {
Date startTime
}
I'm kinda thinking that I want to do this:
def beforeInsert() {
startTime = new DateTime(startTime, user.timeZone).withZone(TimeDateZone.UTC).toDate()
}
But the toDate()
at the end just returns the same startTime
with which I started with (because Date has no time zone information I think).
So, how do I get the Date
the user has provided (taking into consideration their time zone) and persist it to the database as UTC?
I was thinking about doing something like new Date() - user.timeZone.rawOffset
but then I have to handle daylight time savings and it just feels very error prone.