简体   繁体   English

Spring-boot @RequestParam批注将请求中的UTC时区转换为本地时区

[英]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. 我有一个带有控制器的spring-boot应用程序,该控制器处理带有日期作为查询参数传递的某些请求。 Eg URI for request: 例如请求的URI:

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'). 但是,使用@RequestParam批注从URI中解析日期,我在本地时区而不是UTC时区(如“ 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. 我尝试在application.properties中设置jdbctemplate时区,java.util.timezone和spring.jackson.time_zone,但这没有帮助。

@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. 由于传递给@DateTimeFormat的模式中的尾部“ Z”,该值应位于UTC时区。

Any ideas for what I'm missing here? 关于我在这里缺少什么的任何想法?

When you start your application, the VM automatically inherits the default timezone of your local machine. 启动应用程序时,VM会自动继承本地计算机的默认时区。 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);
    }

}

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

相关问题 http请求中的Spring启动时区 - Spring boot Timezone in http request Spring 启动 - 实体:如何添加 CreatedDate(UTC 时区感知)、LastModifiedDate(UTC 时区感知)、CreatedBy、LastModifiedBy - Spring boot - Entity : How to add CreatedDate (UTC timezone aware), LastModifiedDate (UTC timezone aware), CreatedBy, LastModifiedBy Spring-Boot 应用程序中未保留时区更改 - Timezone changes are not being persisted in Spring-Boot application 如何为 Spring Boot JPA 时间戳指定 UTC 时区 - How to specify UTC timezone for Spring Boot JPA Timestamp 在使用 Spring Boot 自动转换 RequestBody 期间,ZonedDateTime 的时区更改为 UTC - Timezone of ZonedDateTime changed to UTC during auto conversion of RequestBody with Spring Boot 如何将 Java 日期时区从服务器本地时区更改为 Spring Boot 本机查询的不同时区? - How to change Java Date timezone from server local timezone to a different timezone for a spring boot native query? Spring Boot将带有时区的日期时间转换为服务器中的``0&#39;&#39;时区 - spring boot convert datetime with timezone to '0' timezone in server Java中的SAML:时区自动从Eastern转换为UTC - SAML in java : timezone converts to UTC from Eastern automatically 将SQL UTC时间戳转换为本地时区Java - Converting SQL UTC timestamp to local timezone Java 将UTC时间从MySQL转换为本地TimeZone - Converting UTC time from MySQL to Local TimeZone
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM