1

I'm facing the following problem, I have a play application with angular and java. In one page the client selects in a calendar some date and time for example 2015-04-03 15:00 this data is put in a JavaScript object as a Date and later this data is submit to my server but it seems the server is converting this date/time to his timezone saving 2015-04-03 16:00 instead of 15:00 as the client side sent.

After I submit the data to the server where it is persisted in the database when I reload the page shows the date with 1 hour less

Sending data to server. Notice that there is a console.info() that prints the date time. It's printing the correct date/time. The date/time selected by user.

$scope.confirmCallback = function () {
                $scope.schedule.client = $scope.client;
                $scope.schedule.type = 'CONTACT';
                console.info($scope.schedule.date); //PRINTS OK DATE/TIME
                ScheduleRepository.create($scope.schedule).then(function () {
                    Alert.success('views.schedule.message.successSaved');
                    $scope.schedule = {};
                    $scope.tableSchedules.reload();
                }, function () {

                });
            }

Here is on my server side on a controller that receives the request. The moment the request gets on the server if I inspect the json I can see that the date time value is different from the one I sent. I guess is something related to timezone on client side and server side.

@Dynamic("CREATE_SCHEDULE, EDIT_SCHEDULE")
public static Result save() {
    try {
        JsonNode request = request().body().asJson();//SHOWS DIFFERENT DATE/TIME
        ScheduleClient scheduleClient = JSONUtils.fromJson(request, ScheduleClient.class);

Any suggestions how to solve this problem? Thanks in advance

Sнаđошƒаӽ
  • 16,753
  • 12
  • 73
  • 90
Viny Machado
  • 582
  • 12
  • 23
  • It is not clear what is being sent to the server, a date or a string representation . This would make a difference. also what is the evidence for the serve changing it. – redge May 10 '15 at 04:43
  • The problem is unclear. Please provide some code. – squill25 May 10 '15 at 04:45
  • @Ignaus Is a date that is sent to the server. A java script object date with the values as mentioned above. When the serve gets the date somehow is changing the date sent. I also add more information on the question. – Viny Machado May 10 '15 at 17:37
  • @redge I added more information on the question. I hope it makes more clear – Viny Machado May 10 '15 at 17:38
  • That's why I asked for some of your code. The problem could be where the server converts the time in the POST request to a java time object, or even before on the client side. – squill25 May 10 '15 at 18:18
  • @Ignaus I just added the code sample as you asked. I hope you can help – Viny Machado May 10 '15 at 18:29
  • Can you please detail what the client timezone and server timezone actually are? Also is what for the database storing the date? It is still unclear what the exact problem is. – redge May 10 '15 at 21:38
  • @redge The server and client are in different timezone something like UTC-3 and UTC-4. I solved the problem sending the date as String and converting to Date on server side instead sending a javascript object Date. I guess Play server was auto converting Date object to its time zone. – Viny Machado May 17 '15 at 04:43

1 Answers1

1

A couple of things to realize:

  • A Date object can't be sent across the wire. It has to be serialized.
  • There's no native date serialization format for JSON, but the best-practice convention is to send an ISO-8601 / RFC3339 serialized string.
  • The JS Date object takes on the time zone of where it is running. So if you call toISOString on it (or if your ScheduleRepository does), it will be converted to UTC using that time zone.
  • On the receiving end, your JSONUtils.fromJson call will deserialize the string value back to whatever object structure your ScheduleClient class uses.
  • If that object also takes on local time behavior, it will use the local time zone of the server.

So you are either seeing the time difference due to comparing local values to UTC values, or by comparing local time values to another time zone's local time.

It's difficult to give more exact advice on what you should do, as you didn't show the important parts of your code. We would need to see the original assignment of the Date object, the serialization code, the string value as sent over the wire, the deserialization code, and the class structure that was being deserialized into. We would also need some context to understand whether your user is selecting a date and time at a particular universal instant, or a particular local-by-their-timezone date and time, or just a calendar date, or what. Context is key, and you haven't provided much to go on.

Community
  • 1
  • 1
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • Thanks for your help. I had to do something like you said. I'm sending the date as String and convert to date on server side. This way I don't have problem with timezone anymore. – Viny Machado May 17 '15 at 04:46