Code
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 可读的格式,因此输入字段 datetime-local 将填充现有的 LocalDateTime 值。
现在的问题是将它转换回 LocalDateTime。 我得到的当前错误是:
Error
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","")。该错误将显示 Text '2017-03-22T01:00' 而不是 Text '2017-03-22 01:00'
最佳答案
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm");
Date date = (Date)formatter.parse(sightingDateString);
谢谢
关于java - 将 html datetime-local 类型输入解析为 java LocalDateTime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43153424/