#Using @JsonFormat to convert a date str in JSON to java.util.Date, converted date's time is wrong ?

1 messages · Page 1 of 1 (latest)

raven elk
#

Hi,
Pretty much same as title.

Here's my DTO :

import java.util.Date;

import com.fasterxml.jackson.annotation.JsonFormat;

public class BookmarkDto {
    
    private int userId;
    
    private int puzzleId;
    
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss.SSS")
    private Date createdAt;

Here's my controller

    @PostMapping("bookmark")
    public ResponseEntity<String> bookmark(@RequestBody BookmarkDto bookmarkDto) {
        try {

            System.out.println(bookmarkDto);
...
...

Here's the json which I am sending in request body

{
    "userId" : 4,
    "puzzleId" : 15,
    "createdAt" : "2020-02-25 09:55:10.000"
}

But for some reason my date is getting converted to "2020-02-25 15:25:10.000000", the time part is wrong.

I am saving this in a db and am attaching my sysout and db data as images.

Can anyone tell me why this is happening and any way to fix it ?

amber cloudBOT
#

<@&1004656351647117403> please have a look, thanks.

amber cloudBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

potent frigate
#

Are you sure it's not compensating for timezone? java.util.Date is probably the wrong type to use: consider LocalDateTime

raven elk
potent frigate
#

Well, look at the values actually being stored. I wouldn't leave it to chance. Date is the wrong type. Use LocalDateTime instead.

raven elk
#

ok lemme check it

potent frigate
#

or OffsetDateTime - most of the JSR310 stuff is much better than Date.

potent frigate
#

well, anywhere, really

raven elk
potent frigate
#

I don't use MySQL, I don't know how it handles dates

#

but the problem LOOKS like you're going to UTC

raven elk
#

hmm ok let me use ldt

#

@potent frigate thanks a ton ! working now !!!

potent frigate
#

my pleasure