Could not parse date: Unparseable date con Spring

di Anonimizzato25783 il
2 risposte
Salve,
sto tentando di inserire dei dati dalla mia applicazione fatta con Spring Boot al mio database mysql, ma quando faccio il submit mi lancia questa eccezione:
Resolved [org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.sql.Date'; nested exception is java.lang.IllegalArgumentException: Could not parse date: Unparseable date: "2018-06-17"]


Ho provato varie soluzioni per riuscire a fare il parsing della data ma nessuno ha avuto successo.

La mia classe Entity ho fatto questo:
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
private Date dataCreazione;
Nella classe Controller questo:
@RequestMapping(value="/aggiungi", method=RequestMethod.POST)
    public String inserimentoarticolo(@RequestParam(value = "dataCreazione") Date date, 
            @ModelAttribute("newArticolo") Articolo a, @ModelAttribute("newCategoria") Categoria c, 
            @ModelAttribute("newIva") Iva i, BindingResult result,
            HttpServletRequest request, HttpServletResponse response, Model model) throws ServletException, IOException {


        c=categoriaService.selectById(Integer.parseInt(request.getParameter("categorie")));
        i=ivaService.selectById(Integer.parseInt(request.getParameter("ive")));

        a.setCodArticolo(Integer.parseInt(request.getParameter("codArticolo")));
        a.setDescrizione(request.getParameter("descrizione"));
        a.setDataCreazione(date);
        a.setCategoria(c);
        a.setIva(i);


        articoloService.insArticolo(a);

        return "index";
    }

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat data = new SimpleDateFormat("dd-MM-yyyy");
        data.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(data, false));
    }
Nella JSP questo:
<form:input id="dataCreazione" path="dataCreazione" type="date" pattern="dd-MM-yyyy" />

Quale può essere il problema?

Grazie

2 Risposte

  • Ovviamente il pattern.
    L'eccezione ti sta dicendo che non riesce a fare il parse della stringa... la stringa è nel formato "yyyy-MM-dd" (lo si vede chiaramente dal suo valore che viene stampato), mentre tu nel pattern hai indicato "dd-MM-yyyy".
  • LeleFT ha scritto:


    Ovviamente il pattern.
    L'eccezione ti sta dicendo che non riesce a fare il parse della stringa... la stringa è nel formato "yyyy-MM-dd" (lo si vede chiaramente dal suo valore che viene stampato), mentre tu nel pattern hai indicato "dd-MM-yyyy".
    ok grazie
Devi accedere o registrarti per scrivere nel forum
2 risposte