Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring @DateTimeFormat is changing timezone while converting @PathVariable to Date

I'm trying to use spring's automatic String to Date conversion when getting date as @PathVariable using @DateTimeFormat. The automatic conversion is done, but for some reason the date is converted to the date I passed minus four hours (Easter Daylight Time, where the server and client are both located).

Example:

URL: .../something/04-10-2016/somename Will result someDate object with value 2016/10/03 20:00:00 (i.e. 8 PM of the previous day, which is 4 hours behind the date I passed.)

How do I avoid this auto timezone conversion. I don't need any timezone information, and this conversion is breaking the behavior I expected.

Here's my code:

@RequestMapping(value = "/something/{someDate}/{name}", method = RequestMethod.GET, headers = "Accept=application/json")
@ResponseBody
public SomeObject getDetails(@PathVariable @DateTimeFormat(pattern = "dd-MM-yyyy") Date someDate,
    @PathVariable String name) {
    // date here comes as GMT - 4
    // more code here
    // return someObject;
}

For I now, I'm working around this by just reading the path variable as a String and using SimpleDateFormatter to convert the String to Date manually.

Any idea on how I can get the auto conversion work without timezone conversion?

like image 415
Suraj Bajaj Avatar asked Oct 04 '16 14:10

Suraj Bajaj


People also ask

How do I convert a string to a date?

Using strptime() , date and time in string format can be converted to datetime type. The first parameter is the string and the second is the date time format specifier. One advantage of converting to date format is one can select the month or date or time individually.

Can we convert string to date in Java?

We can convert String to Date in java using parse() method of DateFormat and SimpleDateFormat classes.

How do I get the current time in spring boot?

getTime(); DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); String formattedDate=dateFormat. format(date); System. out. println("Current time of the day using Calendar - 24 hour format: "+ formattedDate); } . . .


2 Answers

Cause of issue is probably the timezone, java.util.Date instant time is converted back into Eastern Time zone hence you see the value. If you are using Java 8, LocalDate should solve the problem for you.

@PathVariable @DateTimeFormat(pattern = "dd-MM-yyyy") LocalDate date

Following post some options for you neatly

https://www.petrikainulainen.net/programming/spring-framework/spring-from-the-trenches-parsing-date-and-time-information-from-a-request-parameter/

like image 131
kuhajeyan Avatar answered Sep 22 '22 15:09

kuhajeyan


Do Nhu Vy's answer pointed in the right direction; but instead of adding the timezone information, I had to remove it.

TimeZone.setDefault(TimeZone.getTimeZone("UTC"));

Removed this from the config and left the default timezone alone. This worked for me.

like image 26
Suraj Bajaj Avatar answered Sep 22 '22 15:09

Suraj Bajaj