@DateTimeFormat作用相当于SimpleDateFormat,一般用来把前端传递给后端的时间字符串转成对应Date时间类型

在使用@DateTimeFormat,我们需要定义pattern,就是解析字符串的类型

如:

@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")Date startTime

需要注意的是,"yyyy-MM-dd HH:mm:ss"时间格式需要写对,前些天遇到错误,格式写成了"yyyy-MM-dd hh:mm:ss"

如图:

@DateTimeFormat注意点(格式出错)出现异常

org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.util.Date'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.RequestParam @org.springframework.format.annotation.DateTimeFormat java.util.Date] for value '2020-05-20 00:00:00'; nested exception is org.joda.time.IllegalFieldValueException: Cannot parse \"2020-05-20 00:00:00\": Value 0 for clockhourOfHalfday must be in the range [1,12]\r\n\tat

类型不匹配

我们在写代码时候需要多多注意啊

相关文章:

  • 2021-11-17
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-04-24
  • 2021-06-20
猜你喜欢
  • 2022-01-03
  • 2021-10-24
  • 2022-12-23
  • 2021-08-14
  • 2022-12-23
  • 2021-12-22
相关资源
相似解决方案