Home > Software design >  @DateTimeFormat in Spring returns the previous day
@DateTimeFormat in Spring returns the previous day

Time:10-28

I have a method that has a parameter annotated with @DateTimeFormat.

public static void exampleMethod(@ApiParam(value = "date", required = true)
                                 @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date date) {...}

When it gets "2021-10-12", date variable contains "Mon Oct 11 23:00:00 MSK 2021" for some reason. My request doesn't have any time but what I get is the previous date and always 23:00:00.

I really need to use Date data type here, so what can be done to fix this problem?

Thanks in advance.

CodePudding user response:

If anyone ever comes across this problem, I managed to solve it using java.sql.Date (as it is a subclass of java.util.Date) and @JsonFormat annotation at the same time:

public static void exampleMethod(@ApiParam(value = "date", required = true)
                                 @JsonFormat(pattern="yyyy-MM-dd") Date date) {...}
  • Related