1. Overview
1.概述
In this tutorial, we’ll show how to format JSON date fields in a Spring Boot application.
在本教程中,我们将展示如何在Spring Boot应用程序中格式化JSON日期字段。
We’ll explore various ways of formatting dates using Jackson, which Spring Boot uses as its default JSON processor.
我们将探索使用Jackson格式化日期的各种方法,Spring Boot将其作为默认的JSON处理器。
2. Using @JsonFormat on a Date Field
2.在@JsonFormat上使用Date字段
2.1. Setting the Format
2.1.设置格式
We can use the @JsonFormat annotation to format a specific field:
我们可以使用@JsonFormat注释来格式化一个特定的字段。
public class Contact {
// other fields
@JsonFormat(pattern="yyyy-MM-dd")
private LocalDate birthday;
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")
private LocalDateTime lastUpdate;
// standard getters and setters
}
On the birthday field, we use a pattern that renders only the date, while on the lastUpdate field, we also include the time.
在birthday字段上,我们使用一个模式,只渲染日期,而在lastUpdate字段上,我们也包括时间。
We used the Java 8 date types, which are quite handy for dealing with temporal types.
我们使用了Java 8日期类型,这对于处理时间类型相当方便。
Of course, if we need to use the legacy types such as java.util.Date, we can use the annotation in the same way:
当然,如果我们需要使用诸如java.util.Date这样的遗留类型,我们可以以同样的方式使用注解。
public class ContactWithJavaUtilDate {
// other fields
@JsonFormat(pattern="yyyy-MM-dd")
private Date birthday;
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")
private Date lastUpdate;
// standard getters and setters
}
Finally, let’s take a look at the output rendered by using the @JsonFormat with the given date format:
最后,让我们看看使用@JsonFormat与给定的日期格式所呈现的输出。
{
"birthday": "2019-02-03",
"lastUpdate": "2019-02-03 10:08:02"
}
As we can see, using the @JsonFormat annotation is an excellent way to format a particular date field.
正如我们所看到的,使用@JsonFormat 注解是格式化特定日期字段的绝佳方式。
However, we should only use it when we need specific formatting for fields. If we want to have a general format for all dates in our application, there are better ways to achieve this as we’ll see later.
然而,我们应该只在需要字段的特定格式化时才使用它。如果我们想为我们的应用程序中的所有日期设置一个通用的格式,有更好的方法来实现这个目标,我们将在后面看到。
2.2. Setting the Time Zone
2.2.设置时区
If we need to use a particular time zone, we can set the timezone attribute of the @JsonFormat:
如果我们需要使用一个特定的时区,我们可以设置@JsonFormat的timezone属性。
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss", timezone="Europe/Zagreb")
private LocalDateTime lastUpdate;
We don’t need to use it if a type already contains the time zone, for example, with java.time.ZonedDatetime.
如果一个类型已经包含了时区,例如,用java.time.ZonedDatetime,我们就不需要使用它。
3. Configuring the Default Format
3.配置默认格式
While @JsonFormat is powerful on its own, hard-coding the format and time zone can bite us down the road.
虽然 @JsonFormat本身很强大,但硬编码的格式和时区会让我们吃亏。
If we want to configure a default format for all dates in our application, a more flexible way is to configure it in application.properties:
如果我们想为我们的应用程序中的所有日期配置一个默认格式,一个更灵活的方法是在application.properties中进行配置。
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
And if we want to use a specific time zone in our JSON dates, there’s also a property for that:
如果我们想在我们的JSON日期中使用一个特定的时区,也有一个属性用于此。
spring.jackson.time-zone=Europe/Zagreb
Although setting the default format like this is quite handy and straightforward, there’s a drawback to this approach. Unfortunately, it doesn’t work with the Java 8 date types, such as LocalDate and LocalDateTime. We can only use it to format fields of the type java.util.Date or the java.util.Calendar. There is hope, though, as we’ll soon see.
尽管像这样设置默认格式是相当方便和直接的,这种方法有一个缺点。不幸的是,它不适用于Java 8的日期类型,比如LocalDate和LocalDateTime。我们只能用它来格式化java.util.Date或java.util.Calendar类型的域。不过还是有希望的,我们很快就会看到。。
4. Customizing Jackson’s ObjectMapper
4.定制Jackson的ObjectMapper
So, if we want to use Java 8 date types and set a default date format, we need to look at creating a Jackson2ObjectMapperBuilderCustomizer bean:
因此,如果我们想使用Java 8的日期类型和设置一个默认的日期格式,我们需要看看创建一个Jackson2ObjectMapperBuilderCustomizerbean。
@Configuration
public class ContactAppConfig {
private static final String dateFormat = "yyyy-MM-dd";
private static final String dateTimeFormat = "yyyy-MM-dd HH:mm:ss";
@Bean
public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
return builder -> {
builder.simpleDateFormat(dateTimeFormat);
builder.serializers(new LocalDateSerializer(DateTimeFormatter.ofPattern(dateFormat)));
builder.serializers(new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(dateTimeFormat)));
};
}
}
The above example shows how to configure a default format in our application. We have to define a bean and override its customize method to set the desired format.
上面的例子显示了如何在我们的应用程序中配置一个默认格式。我们必须定义一个Bean并覆盖其customize 方法来设置所需的格式。
Although this approach might look a bit cumbersome, the nice thing is that it works for both the Java 8 and the legacy date types.
虽然这种方法可能看起来有点麻烦,但好在它对Java 8和传统的日期类型都适用。
5. Conclusion
5.结论
In this article, we explored a number of ways to format JSON dates in a Spring Boot application.
在这篇文章中,我们探讨了在Spring Boot应用程序中格式化JSON日期的一些方法。
As always, the source code for the examples can be found over on GitHub.
一如既往,这些例子的源代码可以在GitHub上找到。