SpringBoot使用jdk1.8 LocalData日期格式

本文介绍如何在Java项目中使用Spring Boot的Jackson库自定义日期和时间的序列化及反序列化格式,通过创建配置类实现对日期时间格式的统一管理。
@Configuration
public class JacksonConfig {

    /** 默认日期时间格式 */
    public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    /** 默认日期格式 */
    public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
    /** 默认时间格式 */
    public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";


    @Bean
    public ObjectMapper objectMapper(){
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        objectMapper.disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE);
        JavaTimeModule javaTimeModule = new JavaTimeModule();
        javaTimeModule.addSerializer(LocalDateTime.class,new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)));
        javaTimeModule.addSerializer(LocalDate.class,new LocalDateSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)));
        //javaTimeModule.addSerializer(LocalTime.class,new LocalTimeSerializer(DateTimeFormatter.ofPattern(Constants.DEFAULT_TIME_FORMAT)));
        javaTimeModule.addDeserializer(LocalDateTime.class,new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)));
        javaTimeModule.addDeserializer(LocalDate.class,new LocalDateDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)));
        //javaTimeModule.addDeserializer(LocalTime.class,new LocalTimeDeserializer(DateTimeFormatter.ofPattern(Constants.DEFAULT_TIME_FORMAT)));
        objectMapper.registerModule(javaTimeModule).registerModule(new ParameterNamesModule());
        return objectMapper;
    }

}

 

D:\JdK1.8\bin\java.exe -XX:TieredStopAtLevel=1 -noverify -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dspring.jmx.enabled=true -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true "-Dmanagement.endpoints.jmx.exposure.include=*" "-javaagent:D:\toolsz\IntelliJ IDEA 2022.3.2\lib\idea_rt.jar=63813:D:\toolsz\IntelliJ IDEA 2022.3.2\bin" -Dfile.encoding=UTF-8 -classpath D:\JdK1.8\jre\lib\charsets.jar;D:\JdK1.8\jre\lib\deploy.jar;D:\JdK1.8\jre\lib\ext\access-bridge-64.jar;D:\JdK1.8\jre\lib\ext\cldrdata.jar;D:\JdK1.8\jre\lib\ext\dnsns.jar;D:\JdK1.8\jre\lib\ext\jaccess.jar;D:\JdK1.8\jre\lib\ext\jfxrt.jar;D:\JdK1.8\jre\lib\ext\localedata.jar;D:\JdK1.8\jre\lib\ext\nashorn.jar;D:\JdK1.8\jre\lib\ext\sunec.jar;D:\JdK1.8\jre\lib\ext\sunjce_provider.jar;D:\JdK1.8\jre\lib\ext\sunmscapi.jar;D:\JdK1.8\jre\lib\ext\sunpkcs11.jar;D:\JdK1.8\jre\lib\ext\zipfs.jar;D:\JdK1.8\jre\lib\javaws.jar;D:\JdK1.8\jre\lib\jce.jar;D:\JdK1.8\jre\lib\jfr.jar;D:\JdK1.8\jre\lib\jfxswt.jar;D:\JdK1.8\jre\lib\jsse.jar;D:\JdK1.8\jre\lib\management-agent.jar;D:\JdK1.8\jre\lib\plugin.jar;D:\JdK1.8\jre\lib\resources.jar;D:\JdK1.8\jre\lib\rt.jar;C:\Users\lenovo\Desktop\hospital\Department_service\target\classes;D:\localrepository\org\springframework\boot\spring-boot-starter-web\2.3.2.RELEASE\spring-boot-starter-web-2.3.2.RELEASE.jar;D:\localrepository\org\springframework\boot\spring-boot-starter\2.3.2.RELEASE\spring-boot-starter-2.3.2.RELEASE.jar;D:\localrepository\org\springframework\boot\spring-boot\2.3.2.RELEASE\spring-boot-2.3.2.RELEASE.jar;D:\localrepository\org\springframework\boot\spring-boot-starter-logging\2.3.2.RELEASE\spring-boot-starter-logging-2.3.2.RELEASE.jar;D:\localrepository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;D:\localrepository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;D:\localrepository\org\apache\logging\log4j\log4j-to-slf4j\2.13.3\log4j-to-slf4j-2.13.3.jar;D:\localrepository\org\apache\logging\log4j\log4j-api
05-09
D:\Java\JDK1.8\bin\java.exe -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:12617,suspend=y,server=n -javaagent:C:\Users\86159\AppData\Local\JetBrains\IntelliJIdea2023.1\captureAgent\debugger-agent.jar -Dfile.encoding=UTF-8 -classpath "D:\JAVA\JDK1.8\jre\lib\charsets.jar;D:\JAVA\JDK1.8\jre\lib\deploy.jar;D:\JAVA\JDK1.8\jre\lib\ext\access-bridge-64.jar;D:\JAVA\JDK1.8\jre\lib\ext\cldrdata.jar;D:\JAVA\JDK1.8\jre\lib\ext\dnsns.jar;D:\JAVA\JDK1.8\jre\lib\ext\jaccess.jar;D:\JAVA\JDK1.8\jre\lib\ext\jfxrt.jar;D:\JAVA\JDK1.8\jre\lib\ext\localedata.jar;D:\JAVA\JDK1.8\jre\lib\ext\nashorn.jar;D:\JAVA\JDK1.8\jre\lib\ext\sunec.jar;D:\JAVA\JDK1.8\jre\lib\ext\sunjce_provider.jar;D:\JAVA\JDK1.8\jre\lib\ext\sunmscapi.jar;D:\JAVA\JDK1.8\jre\lib\ext\sunpkcs11.jar;D:\JAVA\JDK1.8\jre\lib\ext\zipfs.jar;D:\JAVA\JDK1.8\jre\lib\javaws.jar;D:\JAVA\JDK1.8\jre\lib\jce.jar;D:\JAVA\JDK1.8\jre\lib\jfr.jar;D:\JAVA\JDK1.8\jre\lib\jfxswt.jar;D:\JAVA\JDK1.8\jre\lib\jsse.jar;D:\JAVA\JDK1.8\jre\lib\management-agent.jar;D:\JAVA\JDK1.8\jre\lib\plugin.jar;D:\JAVA\JDK1.8\jre\lib\resources.jar;D:\JAVA\JDK1.8\jre\lib\rt.jar;C:\Users\86159\Desktop\java项目\trainsm\Spring11-1\target\classes;C:\Users\86159\.m2\repository\org\springframework\spring-context\5.2.10.RELEASE\spring-context-5.2.10.RELEASE.jar;C:\Users\86159\.m2\repository\org\springframework\spring-beans\5.2.10.RELEASE\spring-beans-5.2.10.RELEASE.jar;C:\Users\86159\.m2\repository\org\springframework\spring-core\5.2.10.RELEASE\spring-core-5.2.10.RELEASE.jar;C:\Users\86159\.m2\repository\org\springframework\spring-jcl\5.2.10.RELEASE\spring-jcl-5.2.10.RELEASE.jar;C:\Users\86159\.m2\repository\org\springframework\spring-expression\5.2.10.RELEASE\spring-expression-5.2.10.RELEASE.jar;C:\Users\86159\.m2\repository\org\springframework\spring-aop\5.2.10.RELEASE\spring-aop-5.2.10.RELEASE.jar;D:\JAVA\IDEA\IntelliJ IDEA 2023.1\lib\idea_rt.jar" AppForAnnotation Connected to the target VM, address: '127.0.0.1:12617', transport: 'socket' 五月 26, 2025 5:01:45 下午 org.springframework.context.support.AbstractApplicationContext refresh 警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.context.annotation.internalAsyncAnnotationProcessor' defined in class path resource [org/springframework/scheduling/annotation/ProxyAsyncConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.scheduling.annotation.AsyncAnnotationBeanPostProcessor]: Factory method 'asyncAdvisor' threw exception; nested exception is java.lang.IllegalArgumentException: @EnableAsync annotation metadata was not injected Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.context.annotation.internalAsyncAnnotationProcessor' defined in class path resource [org/springframework/scheduling/annotation/ProxyAsyncConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.scheduling.annotation.AsyncAnnotationBeanPostProcessor]: Factory method 'asyncAdvisor' threw exception; nested exception is java.lang.IllegalArgumentException: @EnableAsync annotation metadata was not injected at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:655) at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:483) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1336) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1176) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:556) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516) at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:207) at org.springframework.context.support.PostProcessorRegistrationDelegate.registerBeanPostProcessors(PostProcessorRegistrationDelegate.java:229) at org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:723) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:536) at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:89) at AppForAnnotation.main(AppForAnnotation.java:9) Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.scheduling.annotation.AsyncAnnotationBeanPostProcessor]: Factory method 'asyncAdvisor' threw exception; nested exception is java.lang.IllegalArgumentException: @EnableAsync annotation metadata was not injected at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:650) ... 14 more Caused by: java.lang.IllegalArgumentException: @EnableAsync annotation metadata was not injected at org.springframework.util.Assert.notNull(Assert.java:201) at org.springframework.scheduling.annotation.ProxyAsyncConfiguration.asyncAdvisor(ProxyAsyncConfiguration.java:47) at org.springframework.scheduling.annotation.ProxyAsyncConfiguration$$EnhancerBySpringCGLIB$$49c62e56.CGLIB$asyncAdvisor$0(<generated>) at org.springframework.scheduling.annotation.ProxyAsyncConfiguration$$EnhancerBySpringCGLIB$$49c62e56$$FastClassBySpringCGLIB$$90eb3809.invoke(<generated>) at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331) at org.springframework.scheduling.annotation.ProxyAsyncConfiguration$$EnhancerBySpringCGLIB$$49c62e56.asyncAdvisor(<generated>) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ... 15 more Disconnected from the target VM, address: '127.0.0.1:12617', transport: 'socket' Process finished with exit code 1
最新发布
05-27
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值