2
votes

I have a spring-boot application with a controller that handles some requests with a date passed as a query parameter. E.g 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?

1
A java.util.Date doesn't have a time zone, it is a wrapper around a long with the number of milliseconds since 1-1-1970 00:00 UTC. Its default toString() will render the value in the default JVM time zone. You may want to consider switching to the java.time classes instead.Mark Rotteveel

1 Answers

0
votes

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);
    }

}