Java 使用 spring boot 的 json 输出中的日期格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29027475/
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-08-11 07:13:05  来源:igfitidea点击:

Date format in the json output using spring boot

javarestspring-bootspring-hateoas

提问by Pramod

I am working on spring boot for creating a REST application. And I have a DTO as shown below:

我正在开发用于创建 REST 应用程序的 Spring Boot。我有一个 DTO,如下所示:

public class Subject {

private String uid;
private String number;
private String initials;
private Date dateOfBirth;

And I use Spring-Hateos and the reurn type of my controller is ResponseEntity<Resources<Resource<Subject>>>. I need the date to be displayed in the "yyyy-mm-dd" format.

我使用 Spring-Hateos 并且我的控制器的 reurn 类型是ResponseEntity<Resources<Resource<Subject>>>. 我需要以“yyyy-mm-dd”格式显示日期。

回答by Master Slave

You most likely mean "yyyy-MM-dd" small latter 'm' would imply minutes section.

您最有可能的意思是“yyyy-MM-dd”后面的小“m”表示分钟部分。

You should do two things

你应该做两件事

  • add spring.Hymanson.serialization.write-dates-as-timestamps:falsein your application.propertiesthis will disable converting dates to timestamps and instead use a ISO-8601 compliant format

  • You can than customize the format by annotating the getter method of you dateOfBirthproperty with @JsonFormat(pattern="yyyy-MM-dd")

  • 添加spring.Hymanson.serialization.write-dates-as-timestamps:false您的application.properties这将禁用将日期转换为时间戳,而是使用符合 ISO-8601 的格式

  • 您可以通过注释dateOfBirth属性的 getter 方法来自定义格式@JsonFormat(pattern="yyyy-MM-dd")

回答by Yagnesh Agola

If you have Hymanson integeration with your application to serialize your bean to JSON format, then you can use Hymanson anotation @JsonFormatto format your date to specified format.
In your case if you need your date into yyyy-MM-ddformat you need to specify @JsonFormatabove your field on which you want to apply this format.

如果您的应用程序使用 Hymanson 整数化来将您的 bean 序列化为 JSON 格式,那么您可以使用 Hymanson 注释@JsonFormat将您的日期格式化为指定格式。
在您的情况下,如果您需要将日期转换为yyyy-MM-dd格式,则需要@JsonFormat在要应用此格式的字段上方指定。

For Example :

例如 :

public class Subject {

     private String uid;
     private String number;
     private String initials;

     @JsonFormat(pattern="yyyy-MM-dd")
     private Date dateOfBirth;  

     //Other Code  

}  

From Docs :

从文档:

annotation used for configuring details of how values of properties are to be serialized.

用于配置如何序列化属性值的详细信息的注释。

More Reference Doc

更多参考文档

Hope this helps.

希望这可以帮助。

回答by Willem

If you want to change the format for all dates you can add a builder customizer. Here is an example of a bean that converts dates to ISO 8601:

如果要更改所有日期的格式,可以添加构建器定制器。下面是一个将日期转换为 ISO 8601 的 bean 示例:

@Bean
public Hymanson2ObjectMapperBuilderCustomizer jsonCustomizer() {
    return new Hymanson2ObjectMapperBuilderCustomizer() {
        @Override
        public void customize(Hymanson2ObjectMapperBuilder builder) {
            builder.dateFormat(new ISO8601DateFormat());        
        }           
    };
}

回答by Daniel Higueras

Starting from Spring Boot version 1.2.0.RELEASE , there is a property you can add to your application.propertiesto set a default date format to all of your classes spring.Hymanson.date-format.

从 Spring Boot 版本 1.2.0.RELEASE 开始,您可以添加一个属性来application.properties为所有类设置默认日期格式spring.Hymanson.date-format

For your date format example, you would add this line to your properties file:

对于您的日期格式示例,您可以将此行添加到您的属性文件中:

spring.Hymanson.date-format=yyyy-MM-dd

Reference https://docs.spring.io/spring-boot/docs/1.2.0.RELEASE/reference/html/common-application-properties.html

参考https://docs.spring.io/spring-boot/docs/1.2.0.RELEASE/reference/html/common-application-properties.html