Just like using DefaultConversionService in Spring container, we can expose DefaultFormattingConversionService as a bean to carry out formatting manually.
DefaultFormattingConversionService
package com.logicbig.example; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.support.DefaultConversionService; import org.springframework.format.support.DefaultFormattingConversionService; @Configuration public class AppConfig { @Bean public ConversionService conversionService() { return new DefaultFormattingConversionService(); } @Bean public ClientBean clientBean() { return new ClientBean(); } }
package com.logicbig.example; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.convert.ConversionService; import java.time.Instant; public class ClientBean { @Autowired private ConversionService conversionService; public void showInstantValueAsString(String instantString) { Instant instant = conversionService.convert(instantString, Instant.class); System.out.println(instant); } }
package com.logicbig.example; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class ExampleMain { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); ClientBean clientBean = context.getBean(ClientBean.class); clientBean.showInstantValueAsString("2016-11-15T01:12:05.695Z"); } }
2016-11-15T01:12:05.695Z
Dependencies and Technologies Used:
Version compatibilities of spring-context with this example:
Versions in green have been tested.