How to format the json date format using spring boot
19,683
With Spring Boot, you should be able to set the default way that Jackson formats dates by setting the following property in your application.yml/application.properties:
spring.jackson.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.jackson.databind.util.ISO8601DateFormat)
Related videos on Youtube
Author by
Pramod
I am a java developer, basically work on developing the web applications.
Updated on July 13, 2022Comments
-
Pramod almost 2 years
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.
@Bean @ConditionalOnClass({ ObjectMapper.class, Jackson2ObjectMapperBuilder.class }) public Jackson2ObjectMapperBuilder jacksonBuilder() { Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder(); builder.indentOutput(true).dateFormat(new SimpleDateFormat("yyyy-MM-dd")); return builder; }
And 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.
-
Pramod about 9 yearsAdam Thanks for your reply. It works fine for Spring-boot alone but not with Spring-boot and Spring-Hateoas. Kindly help me out. Or is there are any alternatives to acheive this.
-
adam p about 9 yearsWhere you configure your objectMapper, make sure that this serialization feature is set:
objectMapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);
-
Neil McGuigan over 8 yearsit's almost too obvious