Regarding the release of the date on the local and server

I use CST time on my system, starting from front-end through javascript. I am sending a date today to the spring controller. In the spring controller, through the request parameter, I get the date and convert to date through the @DateTimeFormat annotation, and I get the same date back to the view. I get the exact date I expect.

But, when I test my test environment, which is deployed on amazon server. When I pass today's date, and when I try to get the same date in the foreground, it fits like another date. I wrote the code that I used.

JavaScript:

$http({ method : 'GET', url : urlpath + '/getDate/', params: { date: new Date().toString("MM/dd/yyyy") } }).success(function(response) { console.log("date is"+response) } 

Java Code:

 @RequestMapping(value = "/getdate", method = RequestMethod.GET) public Date getdate(@RequestParam("date") @DateTimeFormat(pattern ="MM/dd/yyyy") throws Exception { return date; } 

Date on my local machine 01-05-2015 23:17 Console expression: Fri May 1, 2015 00:00:00 GMT-0500 (Central Daylight Time) no hh mm and ss since I cropped using the datetimeformat annotation.

In my test environment, I use the same code, but the console is attractive, as shown below. Thu Apr 30 2015 23:00:00 GMT-0500 (Central Daylight Saving Time)

Can anyone solve this problem and tell me the reason for this problem.

+6
source share
1 answer

Time is only HH: MM: SS and that. The time zone comes by default from your computer, unless you implicitly set it differently. So, as you can see, when you send only time, it actually means nothing about real time.

There are 2 solutions.

1) Easier. No matter what you send, send it to the same time zone. For example, UTC. Then, whenever you receive it, use it as a temporary form of UTC. Indirectly. Thus, every part of your system knows that the time sent is UTC and acts accordingly. Never leave it by default. Who knows where (what time zone) one of your computers works.

2) Similar, but with a large number of parameters. Send 2 parameters - time and time zone. It works the same way - you always need the cast time (again, never use it by default) - but at least you know what real-time time is on the sender side, in which case you will see what your test time zone is a computer.

+1
source

Source: https://habr.com/ru/post/986398/


All Articles