LocalDateTime是JDK8中提供的新功能,极大的优化了原生日期时间类的使用。但是第一次使用该类可能会在传值过程中出现格式化的小问题(如:JSON无法解析前端所传格式,序列化时LocalDateTime成为数组等),以下提供简单的解决方案。

推荐方法:

在WebMvcConfigurer实现类下完成以下两步

1). 注册一个Converter<String, LocalDateTime>实现类,其作用是处理于url所携带的参数上(如:@RequestParam@PathVariable )的LocalDateTime参数;

2). 增加一个序列化、反序列化器,作用为处理实体类的的LocalDateTime属性。

P.S. 经过多轮测试,得出了以上方法,由于该方法需要以上两个步骤,作为完美主义者,我曾尝试用一个步骤解决,但并未如愿以偿,若你有更好的方法,还望不吝赐教,先行谢过。

源码如下:

// import ...

@Configuration
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {
	
	/**
	 * 自定义String转LocalDateTime方法,此方法将会作用于url所携带的参数上
	 */
    static class StringToLocalDateTimeConverter implements Converter<String, LocalDateTime> {
        @Override
        public LocalDateTime convert(String s) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            return LocalDateTime.parse(s, formatter);
        }
    }

	/**
	 * 将上述自定义方法进行添加
	 */
    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(new StringToLocalDateTimeConverter());
    }
	
	/**
	 * 增加序列化与反序列化器,它们将作用于实体类的LocalDateTime属性。
	 */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        ObjectMapper objectMapper = new ObjectMapper();
        JavaTimeModule module = new JavaTimeModule();
        module.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(pattern));
        module.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(pattern));
        objectMapper.registerModule(module);
        converters.add(new MappingJackson2HttpMessageConverter(objectMapper));
    }
}

其它方法:

url参数:
给参数加上@DateTimeFormat(此注解来自Spring,无需引入其他包),在pattern中标注约定好的格式即可。

public void Test(@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime time) {
	...
}

实体类:
在实体类中的属性上添加@JsonFormat(此注解来自Jackson,该包被Spring所依赖,无需导入),同样在pattern中标注好格式。

@Data
public class Demo {

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime time;
    
}

由于这种方法需要对代码中所有的LocalDateTime都进行标注,相对麻烦,且耦合度高,所以不作推荐。

GitHub 加速计划 / js / json
19
5
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:5 个月前 )
34665ae6 binary -> binary_t Signed-off-by: Robert Chisholm <robert.chisholm@sheffield.ac.uk> 15 天前
f3dc4684 Bumps [github/codeql-action](https://github.com/github/codeql-action) from 3.28.9 to 3.28.10. - [Release notes](https://github.com/github/codeql-action/releases) - [Changelog](https://github.com/github/codeql-action/blob/main/CHANGELOG.md) - [Commits](https://github.com/github/codeql-action/compare/9e8d0789d4a0fa9ceb6b1738f7e269594bdd67f0...b56ba49b26e50535fa1e7f7db0f4f7b4bf65d80d) --- updated-dependencies: - dependency-name: github/codeql-action dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> 22 天前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐