Search code examples
javaangularjsspringdatequerydsl

Can't convert from String to Timestamp; Wrong format


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.


Solution

  • 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.)