1-Set the global time format
Type of time
For old projects, many classes in springboot use Date type time and do not use JDK8 time classes such as LocalDateTime. Then you can set the json serialization time format directly in the configuration file.
#Configuration in the configuration file jackson: date-format: yyyy-MM-dd HH:mm:ss time-zone: GMT+8
2.JDK8 time
If the time in the project uses JDK8 time classes such as LocalDateTime, LocalDate, LocalTime, etc., and Jackson does not support serialization of these time classes by default, then we need to explicitly configure it.
// Plan 1@Configuration public class JacksonConfig { @Bean public Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder() { Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder(); // Register JavaTimeModule to support JDK 8 time classes JavaTimeModule javaTimeModule = new JavaTimeModule(); // Configure date and time format DateTimeFormatter dateTimeFormatter = ("yyyy-MM-dd HH:mm:ss"); // Customize LocalDateTime serializer and deserializer (, new LocalDateTimeSerializer(dateTimeFormatter)); (, new LocalDateTimeDeserializer(dateTimeFormatter)); // Customize LocalDate serializer and deserializer (if required) (, new LocalDateSerializer(("yyyy-MM-dd"))); (, new LocalDateDeserializer(("yyyy-MM-dd"))); // Customize LocalTime serializer and deserializer (if required) (, new LocalTimeSerializer(("HH:mm:ss"))); (, new LocalTimeDeserializer(("HH:mm:ss"))); (javaTimeModule); // Other global configurations (SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); // Disable timestamp format return builder; } }
// Plan 2@Configuration public class JacksonConfig { @Bean public ObjectMapper objectMapper() { ObjectMapper objectMapper = new ObjectMapper(); // Register JavaTimeModule to support JDK 8 time classes JavaTimeModule javaTimeModule = new JavaTimeModule(); // Configure date and time format DateTimeFormatter dateTimeFormatter = ("yyyy-MM-dd HH:mm:ss"); // Customize LocalDateTime serializer and deserializer (, new LocalDateTimeSerializer(dateTimeFormatter)); (, new LocalDateTimeDeserializer(dateTimeFormatter)); // Customize LocalDate serializer and deserializer (if required) (, new LocalDateSerializer(("yyyy-MM-dd"))); (, new LocalDateDeserializer(("yyyy-MM-dd"))); // Customize LocalTime serializer and deserializer (if required) (, new LocalTimeSerializer(("HH:mm:ss"))); (, new LocalTimeDeserializer(("HH:mm:ss"))); (javaTimeModule); // Other global configurations (SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); // Disable timestamp format return objectMapper; } }
Notice! ! ! When you explicitly configure it through the configuration class, the format configuration and time zone of the Date type in the configuration file will all fail. This is because the custom configuration will override Springboot's automatic configuration. When you detect your custom configuration, the automatic configuration will not take effect again.
3. Make the Date class and JDK8 time class format the time
If your project is messy and these classes are mixed, you need to take the rules on how to serialize the Date class when explicitly configured, as follows
@Configuration public class JacksonConfig { @Bean public ObjectMapper objectMapper() { ObjectMapper objectMapper = new ObjectMapper(); // Set global date format (impact) (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")); // Register JavaTimeModule to support JDK 8 time classes JavaTimeModule javaTimeModule = new JavaTimeModule(); // Configure date and time format DateTimeFormatter dateTimeFormatter = ("yyyy-MM-dd HH:mm:ss"); // Customize LocalDateTime serializer and deserializer (, new LocalDateTimeSerializer(dateTimeFormatter)); (, new LocalDateTimeDeserializer(dateTimeFormatter)); // Customize LocalDate serializer and deserializer (, new LocalDateSerializer(("yyyy-MM-dd"))); (, new LocalDateDeserializer(("yyyy-MM-dd"))); // Customize LocalTime Serializer and Deserializer (, new LocalTimeSerializer(("HH:mm:ss"))); (, new LocalTimeDeserializer(("HH:mm:ss"))); (javaTimeModule); // Disable timestamp format (affects and JDK 8 time classes) (SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); return objectMapper; } }
The above configurations select appropriate serialization/deserialization schemes based on the project situation. These schemes are applicable to the serialization rules of JSON incoming parameters/output parameters. That is to say, when your incoming parameters is in json format, the corresponding time applies to the format you specified, and then jackson will automatically handle it.
When you want to customize the time attribute in a class to return in a specific format, you can use the @JsonFormat annotation, which has priority over the global time format of the project
2-About @DateTimeFormat annotation
@DateTimeFormat annotation is mainly used to format date parameters when entering parameters, especially when processing form submissions or URL request parameters. It does not affect the date format of the parameter (i.e., the response of JSON or other format returned to the client). This annotation is effective for both the Date class and the JDK8 time class
This is the article about setting the time format in Springboot. For more information about setting the time format in Springboot, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!