Spring boot + jpa + LocalDate

0

I'm not able to configure the convert classes in the spring boot to convert the date coming from the view into String to the controler that expects a LocalDate java8, could anyone give me a hint?

    
asked by anonymous 25.11.2017 / 19:04

1 answer

1

I solved it as follows:

@Component
@ConfigurationPropertiesBinding
public class LocalDateFormatter  implements Converter<String, LocalDate> {

        @Override
        public LocalDate convert(String source) {
            if(source==null){
                return null;
            }

            return LocalDate.parse(source, DateTimeFormatter.ofPattern("dd/MM/yyyy"));




        }
    }


import java.text.ParseException;
import java.time.format.DateTimeFormatter;
import java.time.temporal.Temporal;
import java.util.Locale;

import org.springframework.format.Formatter;

public abstract class TemporalFormatter<T extends Temporal> implements Formatter<T> {

    @Override
    public String print(T temporal, Locale locale) {
        DateTimeFormatter formatter = getDateTimeFormatter(locale);
        return formatter.format(temporal);
    }

    @Override
    public T parse(String text, Locale locale) throws ParseException {
        DateTimeFormatter formatter = getDateTimeFormatter(locale);
        return parse(text, formatter);
    }

    private DateTimeFormatter getDateTimeFormatter(Locale locale) {
        return DateTimeFormatter.ofPattern(pattern(locale));
    }

    public abstract String pattern(Locale locale);

    public abstract T parse(String text, DateTimeFormatter formatter);
}
    
05.03.2018 / 00:34