Giriş
Şu satırı dahil ederiz
Örnekimport org.springframework.core.convert.converter.Converter;
Şöyle yaparız
@Componentpublic class LocalDateConverter implements Converter<String, LocalDate> {static final List<String> SUPPORTED_FORMATS = Arrays.asList("dd-MM-yyyy", "yyyy-MM-dd");static final List<DateTimeFormatter> DATE_TIME_FORMATTERS = SUPPORTED_FORMATS.stream().map(DateTimeFormatter::ofPattern).collect(Collectors.toList());@Overridepublic LocalDate convert(String s) {for (DateTimeFormatter dateTimeFormatter : DATE_TIME_FORMATTERS) {try {return LocalDate.parse(s, dateTimeFormatter);} catch (DateTimeParseException ex) {// deliberate empty block so that all parsers run}}throw new DateTimeException(String.format("unable to parse (%s) supported formats are %s",s, String.join(", ", SUPPORTED_FORMATS)));}}
Hiç yorum yok:
Yorum Gönder