zoukankan      html  css  js  c++  java
  • springboot接收日期类型参数

    如果使用Get请求,直接使用对象接收,则可以使用@DateTimeFormat注解进行格式化,如:

    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date startBuyDate;

    如果使用Post请求,@RequestBody接收请求体参数,默认解析日期格式为yyyy-MM-dd HH:mm:ss , 如果需要接收其他格式的参数,则可以使用@JsonFormat注解,如:

    @JsonFormat(timezone="GMT+8",pattern="yyyy-MM-dd")
    private Date buyDate;

    也可以使用java8新增的LocalDate自定义序列化,如:

    @Configuration
    @ConditionalOnClass(ObjectMapper.class)
    @AutoConfigureBefore(JacksonAutoConfiguration.class)
    public class JacksonConfig {
        @Bean
        public Jackson2ObjectMapperBuilderCustomizer customizer() {
            return builder -> {
                builder.locale(Locale.CHINA);
                builder.timeZone(TimeZone.getTimeZone(ZoneId.systemDefault()));
                builder.simpleDateFormat(DatePattern.NORM_DATETIME_PATTERN);
                builder.modules(new TimeModule());
            };
        }
    }
    public class TimeModule extends SimpleModule {
    
        public TimeModule() {
            super(PackageVersion.VERSION);
            this.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN)));
            this.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(DatePattern.NORM_DATE_PATTERN)));
            this.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(DatePattern.NORM_TIME_PATTERN)));
            this.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN)));
            this.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(DatePattern.NORM_DATE_PATTERN)));
            this.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(DatePattern.NORM_TIME_PATTERN)));
        }
    }
    //yyyy-MM-dd HH:mm:ss
    private LocalDateTime buyDateTime;
    
    //yyyy-MM-dd
    private LocalDate buyDate;
    
    //HH:mm:ss
    private LocalTime buyTime;
  • 相关阅读:
    __cdecl, __stdcall, __fastcall,__pascal调用区别
    Windows Hook原理与实现
    C语言四大存储区域总结
    MFC DestroyWindow、OnDestroy、OnClose 程序关闭相关
    VC++动态链接库DLL编程深入浅出"
    windows 安全模型简介
    获取当前焦点窗口进程名
    获取IE URL
    DLL编写中extern “C”和__stdcall的作用
    Django2支持跨域方法
  • 原文地址:https://www.cnblogs.com/jkfeng/p/13801608.html
Copyright © 2011-2022 走看看