-4

Is it possible to adjust the JavaScript Date string value to format of java.sql.Time. I can't seem to find a correct solution for this.

Thanks!

Deniss M.
  • 3,617
  • 17
  • 52
  • 100
  • 5
    what format does `java.sql.Time` have? – Scary Wombat Sep 06 '17 at 08:39
  • 1
    What do you mean with the "format of `java.sql.Time`"? A `java.sql.Time` object does not have a format by itself. – Jesper Sep 06 '17 at 08:40
  • 1
    I want to be able to send a string value which is accepted by the `Time` class – Deniss M. Sep 06 '17 at 08:41
  • 2
    send a string and parse it to Time https://stackoverflow.com/questions/18604408/convert-java-string-to-time-not-date – yvoytovych Sep 06 '17 at 08:42
  • Do you mean a string for `Time.valueOf(String)`? If so, the format should be `hh:mm:ss`. – Ole V.V. Sep 06 '17 at 08:43
  • I'm sending a string with a value from frontend to my api and the api has a field of type `java.sql.Time.` – Deniss M. Sep 06 '17 at 08:44
  • 2
    If you can, avoid the outdated `java.sql.Time` class and prefer the modern `java.time.LocalTime` (or `OffsetTime`) instead. Of course, if it’s an API you cannot change, you need to convert to the class the API expects. – Ole V.V. Sep 06 '17 at 08:46
  • @OleV.V. there is one problem. Some of the date fields in the database have only time and some have only date. – Deniss M. Sep 06 '17 at 08:55

2 Answers2

2

You should serialize you object on server side: es. @JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd'T'HH:mm:ss.sssZ")

and then on the client side you could use moment.js:

moment(stringDate, 'yyyy-MM-dd'T'HH:mm:ss.sssZ').toDate();
NikNik
  • 2,191
  • 2
  • 15
  • 34
  • Thank you! Just what I was looking for. Unfortunately I cannot set the @Оsonformat, because some of the database rows contain invalid formatted Date objects and that will bring in exceptions. – Deniss M. Sep 06 '17 at 08:47
  • By corrupt date object I've meant that some dates have only time and some have only date without time. – Deniss M. Sep 06 '17 at 08:53
-1

this is how to convert to sql date format

var date;
    date = new Date();
    date = date.getUTCFullYear() + '-' +
            ('00' + (date.getUTCMonth() + 1)).slice(-2) + '-' +
            ('00' + date.getUTCDate()).slice(-2) + ' ' +
            ('00' + date.getUTCHours()).slice(-2) + ':' +
            ('00' + date.getUTCMinutes()).slice(-2) + ':' +
            ('00' + date.getUTCSeconds()).slice(-2);