Search in sources :

Example 31 with FormattingConversionService

use of org.springframework.format.support.FormattingConversionService in project spring-data-document-examples by spring-projects.

the class HandlerAdapterConfiguration method conversionService.

@Bean
public ConversionService conversionService() {
    FormattingConversionServiceFactoryBean fb = new FormattingConversionServiceFactoryBean();
    fb.afterPropertiesSet();
    FormattingConversionService cs = fb.getObject();
    return cs;
}
Also used : FormattingConversionServiceFactoryBean(org.springframework.format.support.FormattingConversionServiceFactoryBean) FormattingConversionService(org.springframework.format.support.FormattingConversionService) FormattingConversionServiceFactoryBean(org.springframework.format.support.FormattingConversionServiceFactoryBean) Bean(org.springframework.context.annotation.Bean) LocalValidatorFactoryBean(org.springframework.validation.beanvalidation.LocalValidatorFactoryBean)

Example 32 with FormattingConversionService

use of org.springframework.format.support.FormattingConversionService in project spring-boot by spring-projects.

the class WebMvcAutoConfigurationTests method customDateFormat.

@Test
void customDateFormat() {
    this.contextRunner.withPropertyValues("spring.mvc.format.date:dd*MM*yyyy").run((context) -> {
        FormattingConversionService conversionService = context.getBean(FormattingConversionService.class);
        Date date = Date.from(ZonedDateTime.of(1988, 6, 25, 20, 30, 0, 0, ZoneId.systemDefault()).toInstant());
        assertThat(conversionService.convert(date, String.class)).isEqualTo("25*06*1988");
    });
}
Also used : FormattingConversionService(org.springframework.format.support.FormattingConversionService) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 33 with FormattingConversionService

use of org.springframework.format.support.FormattingConversionService in project spring-boot by spring-projects.

the class WebMvcAutoConfigurationTests method defaultDateTimeFormat.

@Test
void defaultDateTimeFormat() {
    this.contextRunner.run((context) -> {
        FormattingConversionService conversionService = context.getBean(FormattingConversionService.class);
        LocalDateTime dateTime = LocalDateTime.of(2020, 4, 28, 11, 43, 10);
        assertThat(conversionService.convert(dateTime, String.class)).isEqualTo(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT).format(dateTime));
    });
}
Also used : LocalDateTime(java.time.LocalDateTime) FormattingConversionService(org.springframework.format.support.FormattingConversionService) Test(org.junit.jupiter.api.Test)

Example 34 with FormattingConversionService

use of org.springframework.format.support.FormattingConversionService in project spring-boot by spring-projects.

the class WebMvcAutoConfigurationTests method defaultDateFormat.

@Test
void defaultDateFormat() {
    this.contextRunner.run((context) -> {
        FormattingConversionService conversionService = context.getBean(FormattingConversionService.class);
        Date date = Date.from(ZonedDateTime.of(1988, 6, 25, 20, 30, 0, 0, ZoneId.systemDefault()).toInstant());
        // formatting conversion service should use simple toString()
        assertThat(conversionService.convert(date, String.class)).isEqualTo(date.toString());
    });
}
Also used : FormattingConversionService(org.springframework.format.support.FormattingConversionService) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 35 with FormattingConversionService

use of org.springframework.format.support.FormattingConversionService in project spring-boot by spring-projects.

the class WebMvcAutoConfigurationTests method customDateTimeTimeFormat.

@Test
void customDateTimeTimeFormat() {
    this.contextRunner.withPropertyValues("spring.mvc.format.date-time=yyyy-MM-dd HH:mm:ss").run((context) -> {
        FormattingConversionService conversionService = context.getBean(FormattingConversionService.class);
        LocalDateTime dateTime = LocalDateTime.of(2020, 4, 28, 11, 43, 10);
        assertThat(conversionService.convert(dateTime, String.class)).isEqualTo("2020-04-28 11:43:10");
    });
}
Also used : LocalDateTime(java.time.LocalDateTime) FormattingConversionService(org.springframework.format.support.FormattingConversionService) Test(org.junit.jupiter.api.Test)

Aggregations

FormattingConversionService (org.springframework.format.support.FormattingConversionService)38 Test (org.junit.jupiter.api.Test)27 DefaultFormattingConversionService (org.springframework.format.support.DefaultFormattingConversionService)13 Date (java.util.Date)8 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)8 Locale (java.util.Locale)6 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)6 DerivedTestBean (org.springframework.beans.testfixture.beans.DerivedTestBean)6 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)6 IndexedTestBean (org.springframework.beans.testfixture.beans.IndexedTestBean)6 TestBean (org.springframework.beans.testfixture.beans.TestBean)6 NumberStyleFormatter (org.springframework.format.number.NumberStyleFormatter)6 ParseException (java.text.ParseException)5 LocalDateTime (java.time.LocalDateTime)4 LocalTime (java.time.LocalTime)4 List (java.util.List)4 Bean (org.springframework.context.annotation.Bean)4 StringReader (java.io.StringReader)3 Document (org.dom4j.Document)3 Element (org.dom4j.Element)3