Problems converting date to calendar

1

Trying to convert date from a date to calendar. Using springBoot. I was not clear about the true cause of the exception. Could someone help?

@SpringBootApplication
public class Boot
{
   public static void main(String[] args)
   {
      SpringApplication.run(Boot.class, args);
   }
   @Bean
    public FormattingConversionService mvcConversionService() {
        DefaultFormattingConversionService conversionSeervice = new DefaultFormattingConversionService(true);
        DateFormatterRegistrar registrar = new DateFormatterRegistrar();
        registrar.setFormatter(new DateFormatter("yyyy-MM-dd"));
        registrar.registerFormatters(conversionSeervice);
        return conversionSeervice;

    }
}

No Model

@DateTimeFormat(tried with and without pattern)
private Calendar releaseDate;

in the JSP

<div>
    <label for="releaseDate">Data de Lançamento</label>
     <form:input path="releaseDate" type="date" id="releaseDate"/>
     <form:errors path="releaseDate"/>
 </div>

Exception:

  

Failed to convert property value of type java.lang.String to required type java.util.Calendar for property releaseDate;   nested exception is org.springframework.core.convert.ConversionFailedException:   Failed to convert from type [java.lang.String] to type   [@ org.springframework.format.annotation.DateTimeFormat java.util.Calendar]   for value 2017-12-06; nested exception is java.lang.IllegalArgumentException:   Parse attempt failed for value [2017-12-06]

    
asked by anonymous 06.12.2017 / 11:46

1 answer

0

Actually, what you are converting is String to Calendar. As much as in your jsp, it is type = date, what arrives for spring is a String.

I honestly can not think of a useful use currently for the Calendar class. I find Java's date API classes infinitely better than Calendar, and I advise you to learn how to use them. Ex: java.util.LocalDate

Now answering your question, I'll assume you're using Jackson as the JSON serializer / deserializer. In this case, create a deserializer so that Spring, through Jackson, can convert this String to a Calendar. Here's an example of how to do it:

public class CalendarDeserializer extends JsonDeserializer<Calendar> {

    private static final SimpleDateFormat FORMATTER = new SimpleDateFormat("yyyy-MM-dd");

    @Override
    public Calendar deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {

        String dateAsString = jsonParser.getText();

        Date date = tryParseDate(dateAsString);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar;
    }

    private Date tryParseDate(String dateAsString) throws IOException {
        Date date;
        try {
            date = FORMATTER.parse(dateAsString);
        } catch (ParseException e) {
            throw new IOException(e);
        }
        return date;
    }

}

Then you just have to indicate that your Deserializer will be used for that property, annotating it in your Model, as follows:

@JsonDeserialize(using = CalendarDeserializer.class)
private Calendar releaseDate;

That'll probably work.

    
06.12.2017 / 13:00