0
votes

I'm getting the following error when trying to pass a date object from AngularJS to java spring backend:

Failed to convert from type [java.lang.String] to type [@javax.persistence.Column java.sql.Timestamp] for value '2018-06-12T22:00:00.000Z'; nested exception is java.lang.IllegalArgumentException: Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]

So far I tried to format the date object to a string in the expected format:

$filter('date')(date, "yyyy-MM-dd hh:mm:ss");

which leads to an error telling:

Error: [ngModel:datefmt] Expected 2018-06-13 12:00:00 to be a date

Seems like I need to pass a date object but I can't find a way to influence the date format AngularJS is attempting to convert to.

2
Can you add some code? - delephin
The string you got parses nicely into a java.time.Instant, even without any formatter since the format is the default ISO 8601. How you persuade Spring into doing that and saving the Instant into the database I don’t know (sorry). - Ole V.V.

2 Answers

3
votes

java.time.Instant

Your backend service is outdated, using a legacy class java.sql.Timestamp. That class was supplanted years ago by java.time.Instant.

If you make that change your backend to use Instant, you’ll have no problem passing a String such as 2018-06-12T22:00:00.000Z. That string is using a standard format defined in ISO 8601. That format is the ideal way to exchange date-time values as text.

The java.time classes use ISO 8601 formats by default. So no need to specify a formatting pattern.

I know Hibernate has been updated to support the java.time classes. I don’t know about JPA. (I don’t use either.)

-1
votes

You can convert a date string to a date object using any format like this:

Date parseDateString(String dateString){
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    Date date = dateFormat.parse(dateString);
}

You can read more about it in the documentation