Format date in Angular for Java.util.Date

1

I have the following date generated in AngularJS: 2015-09-14T18:38:03.637Z when I try to give a POST the following error occurs in the backend:

  

Caused by: java.text.ParseException: Unparseable date:   "2015-09-14T18: 38: 03.637Z" at java.text.DateFormat.parse (Unknown   Source) at   br.com.caelum.vraptor.serialization.gson.DateGsonConverter.deserialize (DateGsonConverter.java:59)     ... 56 more

In angular I do this: contato.data = new Date(); the attribute that will receive that date in backend is of type Java.util.Date . I've tried it this way:

contato.data = $filter('date')(new Date(), 'yyyy-MM-dd');

But it did not work. How can I convert this date?

Method that does POST:

    @Post
    @Path(value = "/salvar")
    @Consumes(value = "application/json", options = WithoutRoot.class)
    public void salvar(Contato contato) {
        System.out.println("Empresa: " + contato.getNome());
        contatoDAO.salvar(contato);

    }
    
asked by anonymous 14.09.2015 / 20:50

1 answer

1

Dates submitted to backend of need be in the format expected by the serializer used in your application.

Apparently, gson is being used, and the format it expects is usually of type Sep, 14 2015 6:38:03 PM .

A good library for parsing javascript is moment.js . With this library, parse would be something like this:

contato.data = moment((new Date()).getTime()).format('MMM D, YYYY h:mm:ss A');

If it does not work, the expected format might be different from the one I mentioned above. To get the exact format, make a GET to some endpoint of the application that returns a json whose any of the attributes of the object is generated from a Java.util.Date .

    
15.09.2015 / 20:49