Jils
Jils

Reputation: 783

Dates displayed in the server are different than I expected

I try to display dates that I send via HTTP request in my webapp.

In the locale environment the dates sent are displayed correctly, but when the webapp is deployed on the server the dates displayed correspond to the current date on the server not the dates I sent.

Example of a http request: HTTP request sent: http ://...?persoDate="03/10/2013 11:29:55"

Here's my code:

def initDate = new SimpleDateFormat("d/M/yyyy H:m:s").parse(params["persoDate"].replaceAll(/"/, ''))

MyInstance.addToAbList(new BOSSession(setDateSession:initDate))

MyInstance.save(flush: true)

Upvotes: 0

Views: 331

Answers (1)

Olaf Kock
Olaf Kock

Reputation: 48057

You're missing the timezone this date/time is expected to be in. On your local computer it's probably what you expect - it's your computer anyway and most likely it shows the time where you currently are. The server however might be on UTC or any other timezone and interpret the date as relative to that timezone. Now if you parse it, then convert it to your local timezone, you'll be up to 24h off, depending on the "distance" your local computer and the server have in terms of timezones.

Following your comments:

The java type Date is always without timezone, you'll need to go through Calendar in order to do timezone processing as well. This is what might happen either during storage or display of the dates. First you'll need to find out where the problem is: i.e. in parsing, in storing (what does actually end up in your database) or in display of the values that have been written do the database. Also, you might need to explicitly save the intended timezone to the database to reproduce it later when you display the values.

Steps to test:

  • What's your local computer's timezone? What's the server's timezone? The DB server's?
  • What is stored to the database? Does it contain a timezone?
  • What do you read from the database?
  • What gets displayed to the user? Depending on the formatting that you do, this might do the timezone calculation as well.

As soon as you find out which of the different steps need the correction, you have a point to look at deeper. I can't tell from here on which of the steps your problems are, thus no code sample... (also, I've not exercised my groovy muscle in a long time) sorry.

If you're in a web application, make sure that you know what timezone the user actually expects. If you only ever need to handle a single timezone (and are extra extra extra sure that there won't be another) just set your server to the timezone you want to handle and all of your users are in. This assumption will break sooner or later, but for now you're set :(

Upvotes: 1

Related Questions