将html datetime-local类型输入解析为java LocalDateTime

时间:2017-04-01 06:06:39

标签: java jsp datetime

  

代码

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。 我得到的当前错误是:

  

错误

Request processing failed; nested exception is 
java.time.format.DateTimeParseException: Text '2017-03-22 01:00' could not be parsed at index 10

我也试过没有替换(&#34; T&#34;,&#34;&#34;)。错误将显示文字&#39; 2017-03-22T01:00&#39;而不是文字&#39; 2017-03-22 01:00&#39;

1 个答案:

答案 0 :(得分:2)

DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm");
Date date = (Date)formatter.parse(sightingDateString); 

由于