1

I have this date "2018-05-30T16:19:58.016Z" coming from my Angular app. In Spring, the field date is as follows :

@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
private Date date;

The date is well stored, but with this format YYYY-MM-dd. enter image description here

Is there anything that I'm missing ?

androniennn
  • 2,979
  • 10
  • 45
  • 101

2 Answers2

1

You probably have to specify the date format going out to the storage, as @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") only specifies the date format for parsing the date into the Date object. Even if your Date has all of the seconds and timezone information, the default Date toString() is still

Formats a date in the date escape format yyyy-mm-dd.

according to the Java 8 docs. So if you are using that, it would most likely drop all that extra information on conversion.

You can look at this Convert java.util.Date to String for information on how to get a Date to a formatted String.

Will M.
  • 1,814
  • 14
  • 26
  • So you suggest to change the date format in the setter? – androniennn May 30 '18 at 17:28
  • @androniennn how are you storing your Date in your database? maybe edit your question to add that – Will M. May 30 '18 at 17:33
  • I save the object, and the date field is stored. I have no problem with that. I think that I will try to make as you suggested: a small transformation in date before setting it. – androniennn May 30 '18 at 17:35
  • 1
    @androniennn when you say `I save the object, and the date field is stored` that might be the issue. If your "save the object" code uses toString() to save the date object, then, according to the javadocs, it would be saving it as yyyy-mm-dd. Without knowing what you are using to save the object, I can't say for sure. – Will M. May 30 '18 at 17:38
1

MySql date type can't hold data with timestamp. It has to be datetime in order to contain date time with timestamp data.

fluffyBatman
  • 5,304
  • 3
  • 19
  • 24