简体   繁体   English

服务器端更改客户端发送的日期/时间

[英]Server side changes date/time sent by client

I'm facing the following problem, I have a play application with angular and java.我面临以下问题,我有一个带有 angular 和 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.在一个页面中,客户端在日历中选择某个日期和时间,例如2015-04-03 15:00将此数据作为日期放入 JavaScript 对象中,稍后将此数据提交到我的服务器,但似乎服务器正在转换这个日期/时间到他的时区保存 2015-04-03 16:00而不是客户端发送的15:00

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在我将数据提交到服务器后,当我重新加载页面时,它会保留在数据库中,显示的日期减少了 1 小时

Sending data to server.向服务器发送数据。 Notice that there is a console.info() that prints the date time.请注意,有一个打印日期时间的 console.info()。 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.如果我检查 json,请求到达服务器的那一刻,我可以看到日期时间值与我发送的不同。 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提前致谢

A couple of things to realize:需要意识到的几件事:

  • A Date object can't be sent across the wire.不能通过网络发送Date对象。 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. JSON 没有本地日期序列化格式,但最佳实践约定是发送ISO-8601 / RFC3339序列化字符串。
  • The JS Date object takes on the time zone of where it is running. JS Date对象采用它运行所在的时区。 So if you call toISOString on it (or if your ScheduleRepository does), it will be converted to UTC using that time zone.因此,如果您对其调用toISOString (或者如果您的ScheduleRepository调用),它将使用该时区转换为 UTC。
  • On the receiving end, your JSONUtils.fromJson call will deserialize the string value back to whatever object structure your ScheduleClient class uses.在接收端,您的JSONUtils.fromJson调用会将字符串值反序列化回您的ScheduleClient类使用的任何对象结构。
  • 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.因此,由于将本地值与 UTC 值进行比较,或者通过将本地时间值与另一个时区的本地时间进行比较,您会看到时差。

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.我们需要查看Date对象的原始分配、序列化代码、通过网络发送的字符串值、反序列化代码以及被反序列化成的类结构。 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.上下文是关键,您没有提供太多内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM