java 如何使用spring boot格式化json日期格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29077617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 14:39:35  来源:igfitidea点击:

How to format the json date format using spring boot

javajsonspringrestspring-boot

提问by Pramod

I am working on spring boot and gradle for creating a rest service. Now I need to format the json date in the form of "yyyy-MM-dd", i.e, the format should be dateOfBirth: "16-03-2015", but I am getting "dateOfBirth: -751181400000". I added the below piece of code in my Apllication.java class, but still not able to get the desired output.

我正在研究 spring boot 和 gradle 以创建休息服务。现在我需要以“yyyy-MM-dd”的形式格式化json日期,即格式应该是dateOfBirth:“16-03-2015”,但我得到“dateOfBirth:-751181400000”。我在我的 Application.java 类中添加了以下代码段,但仍然无法获得所需的输出。

@Bean
@ConditionalOnClass({ ObjectMapper.class, Hymanson2ObjectMapperBuilder.class })
public Hymanson2ObjectMapperBuilder HymansonBuilder()
{ 
    Hymanson2ObjectMapperBuilder builder = new Hymanson2ObjectMapperBuilder(); 
    builder.indentOutput(true).dateFormat(new SimpleDateFormat("yyyy-MM-dd")); 
    return builder; 
}

And Application.java:

和 Application.java:

@Configuration
@Import(SubjectServiceConfig.class)
@EnableAutoConfiguration
@EnableEurekaClient
@ComponentScan({"com.billing"})
@EnableWebMvc
@EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL)
public class Application {
public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
}
}

Kindly help me out in resolving this issue.

请帮助我解决这个问题。

回答by adam p

With Spring Boot, you should be able to set the default way that Hymanson formats dates by setting the following property in your application.yml/application.properties:

使用 Spring Boot,您应该能够通过在 application.yml/application.properties 中设置以下属性来设置 Hymanson 格式化日期的默认方式:

spring.Hymanson.date-format= # Date format string (e.g. yyyy-MM-dd HH:mm:ss), or a fully-qualified date format class name (e.g. com.fasterxml.Hymanson.databind.util.ISO8601DateFormat)