Format date in Angular for Java.util.Date

Asked

Viewed 1,066 times

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 happens 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

At the angle I do as follows: contato.data = new Date(); the attribute that will receive that date in backend is the type Java.util.Date. I’ve tried that way:

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

But it didn’t work. How can I convert this date?

Method that makes the 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);

    }

1 answer

1


The dates sent to the backend of must be in the format expected by the serializer used in your application.

Apparently, in this case, the gson is being used, and the format expected by it is usually of the type Sep, 14 2015 6:38:03 PM.

A good library to do the parse of the date of javascript is the Moment.js. With this library, the parse would be something like that:

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

If it doesn’t work, it may be that the expected format is different from what I mentioned above. To get the exact format, make a GET for some endpoint of the application that returns a json whose some of the object attributes are generated from a Java.util.Date.

Browser other questions tagged

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