Search in sources :

Example 1 with Printer

use of org.springframework.format.Printer in project spring-framework by spring-projects.

the class FormattingConversionServiceTests method formatFieldForAnnotationWithSubclassAsFieldType.

@Test
public void formatFieldForAnnotationWithSubclassAsFieldType() throws Exception {
    formattingService.addFormatterForFieldAnnotation(new JodaDateTimeFormatAnnotationFormatterFactory() {

        @Override
        public Printer<?> getPrinter(org.springframework.format.annotation.DateTimeFormat annotation, Class<?> fieldType) {
            assertEquals(MyDate.class, fieldType);
            return super.getPrinter(annotation, fieldType);
        }
    });
    formattingService.addConverter(new Converter<MyDate, Long>() {

        @Override
        public Long convert(MyDate source) {
            return source.getTime();
        }
    });
    formattingService.addConverter(new Converter<MyDate, Date>() {

        @Override
        public Date convert(MyDate source) {
            return source;
        }
    });
    formattingService.convert(new MyDate(), new TypeDescriptor(ModelWithSubclassField.class.getField("date")), TypeDescriptor.valueOf(String.class));
}
Also used : Printer(org.springframework.format.Printer) ReadablePartialPrinter(org.springframework.format.datetime.joda.ReadablePartialPrinter) Date(java.util.Date) LocalDate(org.joda.time.LocalDate) TypeDescriptor(org.springframework.core.convert.TypeDescriptor) JodaDateTimeFormatAnnotationFormatterFactory(org.springframework.format.datetime.joda.JodaDateTimeFormatAnnotationFormatterFactory) Test(org.junit.Test)

Aggregations

Date (java.util.Date)1 LocalDate (org.joda.time.LocalDate)1 Test (org.junit.Test)1 TypeDescriptor (org.springframework.core.convert.TypeDescriptor)1 Printer (org.springframework.format.Printer)1 JodaDateTimeFormatAnnotationFormatterFactory (org.springframework.format.datetime.joda.JodaDateTimeFormatAnnotationFormatterFactory)1 ReadablePartialPrinter (org.springframework.format.datetime.joda.ReadablePartialPrinter)1