Search in sources :

Example 1 with DateTimeParser

use of org.springframework.format.datetime.joda.DateTimeParser in project spring-framework by spring-projects.

the class FormattingConversionServiceTests method formatFieldForTypeWithPrinterParserWithCoercion.

@Test
public void formatFieldForTypeWithPrinterParserWithCoercion() throws ParseException {
    formattingService.addConverter(new Converter<DateTime, LocalDate>() {

        @Override
        public LocalDate convert(DateTime source) {
            return source.toLocalDate();
        }
    });
    formattingService.addFormatterForFieldType(LocalDate.class, new ReadablePartialPrinter(DateTimeFormat.shortDate()), new DateTimeParser(DateTimeFormat.shortDate()));
    String formatted = formattingService.convert(new LocalDate(2009, 10, 31), String.class);
    assertEquals("10/31/09", formatted);
    LocalDate date = formattingService.convert("10/31/09", LocalDate.class);
    assertEquals(new LocalDate(2009, 10, 31), date);
}
Also used : ReadablePartialPrinter(org.springframework.format.datetime.joda.ReadablePartialPrinter) DateTimeParser(org.springframework.format.datetime.joda.DateTimeParser) LocalDate(org.joda.time.LocalDate) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Aggregations

DateTime (org.joda.time.DateTime)1 LocalDate (org.joda.time.LocalDate)1 Test (org.junit.Test)1 DateTimeParser (org.springframework.format.datetime.joda.DateTimeParser)1 ReadablePartialPrinter (org.springframework.format.datetime.joda.ReadablePartialPrinter)1