简体   繁体   中英

Spring-boot @RequestParam annotation converts UTC timezone in request to local timezone

I have a spring-boot application with a controller that handles some requests with a date passed as a query parameter. Eg URI for request:

http://localhost:8081/test/read?from=2018-01-01T00:00:00Z

However, using the @RequestParam annotation to parse the date from the URI, I am getting a date in the local timezone, not the UTC timezone (as indicated by the 'Z').

I've tried setting the jdbctemplate time zone, the java.util.timezone and the spring.jackson.time_zone in application.properties, but that didn't help.

@RequestMapping(value = "test/read", method = RequestMethod.GET)
    public ResponseEntity<String> get(
            @RequestParam(value="from") @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss'Z'") final Date from)
    // value of date in 'from' is in local timezone

The value should be in the UTC timezone, due to the trailing 'Z' in the pattern passed to @DateTimeFormat.

Any ideas for what I'm missing here?

When you start your application, the VM automatically inherits the default timezone of your local machine. You can update the default timezone for your application as following.

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        TimeZone.setDefault(TimeZone.getTimeZone("UTC"));

        SpringApplication.run(Application.class, args);
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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