Problems converting date to Calendar

Asked

Viewed 299 times

1

Trying to convert date from date to date. Using springBoot. I didn’t have clarity about the true cause of the exception. Someone could 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;

    }
}

in the 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]

  • How’s your DateFormatterRegistrar?

  • what you want to know about him?

  • I want to know how you convert the date.

  • He was being written by Spring. I disabled the override, but now I’m with another exception I used public Static void main(String[] args) { new Springapplicationbuilder(Boot.class) . initializers((Genericapplicationcontext c) -> c.setAllowBeanDefinitionOverriding(false) ) . run(args); }

  • but now I’m with another exception org.springframework.Beans.factory.Beandefinitionstoreexception: Invalid bean Definition with name 'mvcConversionService' defined in class path Resource [org/springframework/boot/autoconfigure/web/Webmvcautoconfiguration$Enablewebmvcconfiguration.class]: Cannot Register bean Definition, which is logical because I blocked the override

1 answer

0

Actually, what you’re converting is from String to Calendar. As much as in your jsp it is type = date, what comes to spring is a String.

I honestly can’t think of a useful use for the Calendar class today. I find the java 8 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’m going to assume you’re using Jackson as a JSON serializer/deserializer. In this case, create an deserializer so that Spring, through Jackson, can convert this String into a Calendar. Follow 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 need to indicate that your Deserializer will be used for that Presoperty, noting there in your Model, as follows:

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

Then it’ll probably work.

  • did not roll, because then is enabled the method loaded by boot that contains the web Browser (added in the pom) and generates exception before it is possible to arrive in the converter.

Browser other questions tagged

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