[英]java.util.Date from <input type=“datetime-local” />
[英]Parsing html datetime-local type input into java LocalDateTime
码
JSP:
<div class="col-md-4 col-sm-6 col-xs-12">
<fmt:formatDate value="${sighting.sightingDateAsDate}" var="sightingDateString" pattern="yyyy-MM-dd'T'hh:mm:ss"/>
<sf:input type="datetime-local" class="add-form form-control" path="sightingDate" value="${sightingDateString}"/>
</div>
Controller:
String sightingDateString = request.getParameter("sightingDate");
LocalDateTime sightingDate = LocalDateTime.parse(sightingDateString.replace("T"," "), DateTimeFormatter.ISO_DATE_TIME);
我正在处理此编辑表单。 fmt:formatDate会将SightingDate转换为可由HTML读取的格式,因此将使用现有LocalDateTime值填充输入字段datetime-local。
现在,问题是将其转换回LocalDateTime。 我得到的当前错误是:
错误
Request processing failed; nested exception is
java.time.format.DateTimeParseException: Text '2017-03-22 01:00' could not be parsed at index 10
我也尝试了不使用replace(“ T”,“”)。 该错误将显示文本'2017-03-22T01:00'而不是文本'2017-03-22 01:00'
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm");
Date date = (Date)formatter.parse(sightingDateString);
谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.