无法将 java.lang.String 类型的值转换为所需的 java.util.Date 类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39600202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Failed to convert value of type java.lang.String to required type java.util.Date
提问by Extony
<form th:action="@{home}" method="get">
<div class="form-group">
<label>from date:</label> <input type="date" pattern="yyyy-MM-dd" name="d1" th:value="${d1}" />
<label>to date:</label> <input type="date" pattern="yyyy-MM-dd" name="d2" th:value="${d2}" />
<button type="submit">Trouver</button>
</div>
</form>
this is the controller code part:
这是控制器代码部分:
@RequestParam(name = "d1", defaultValue = "1900-01-01") @DateTimeFormat(pattern = "yyyy-MM-dd") Date d1,
@RequestParam(name = "d2", defaultValue = "2200-01-01") @DateTimeFormat(pattern = "yyyy-MM-dd") Date d2){
Im getting this message:
我收到这条消息:
There was an unexpected error (type=Bad Request, status=400). 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 'Wed Jun 08 00:00:00 WET 2016'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [Wed Jun 08 00:00:00 WET 2016]
出现意外错误(类型=错误请求,状态=400)。无法将类型 [java.lang.String] 的值转换为所需类型 [java.util.Date];嵌套异常是 org.springframework.core.convert.ConversionFailedException:无法从类型 [java.lang.String] 转换为类型 [@org.springframework.web.bind.annotation.RequestParam @org.springframework.format.annotation.DateTimeFormat java.util.Date] 值 'Wed Jun 08 00:00:00 WET 2016'; 嵌套异常是 java.lang.IllegalArgumentException:解析尝试失败的值 [Wed Jun 08 00:00:00 WET 2016]
回答by baao
The pattern you give to your html input elements doesn't work as you expect. You are not setting a format for your date with this, and as you can see from your error message, the date that spring is trying to parse is
您为 html 输入元素提供的模式无法按预期工作。您没有为此设置日期格式,正如您从错误消息中看到的那样,spring 尝试解析的日期是
Wed Jun 08 00:00:00 WET 2016
not any date in the format you have set in both your html and in your controller (the html pattern doesn't modify the format that gets sent, it's there for validation purposes).
不是您在 html 和控制器中设置的格式的任何日期(html 模式不会修改发送的格式,它用于验证目的)。
I've never worked with that, but you should either
我从来没有用过,但你也应该
- just remove the complete pattern and format and see if that works (I guess it could)
- set the correct date format in your controller pattern, according to the date format I posted above (and your error message).
- 只需删除完整的模式和格式,看看是否有效(我想可以)
- 根据我上面发布的日期格式(以及您的错误消息),在您的控制器模式中设置正确的日期格式。
Here:
这里:
@DateTimeFormat(pattern = "yyyy-MM-dd")
回答by N.Neupane
You have correctly set pattern in the Controller Class with the annotation @DateTimeFormat(pattern = "yyyy-MM-dd"). And please also make sure that you have: imported two required patterns in your Model/Entity Class as:
您已使用注释@DateTimeFormat(pattern = "yyyy-MM-dd") 在控制器类中正确设置了模式。并且还请确保您已经: 在您的模型/实体类中导入了两个必需的模式:
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Temporal(TemporalType.DATE)
private Date date;
Hope it does work.cause It worked on mine.
希望它起作用。因为它对我的起作用。