Spring boot + jpa + Localdate

Asked

Viewed 501 times

0

I am unable to configure the classes convert in spring boot to convert the date that comes from the view into String to the controler that expects a Localdate java8, Someone could give me a hint ?

  • What JPA implementation are you using? You can enter your code?

  • Friend I managed to solve

1 answer

1

I decided 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);
}
  • Is that the solution or is it a supplementary code to the question?

  • 1

    Was the solution !!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.