无法将日期时间从 jsp 表单传递到 google appengine

unable to pass datetime to google appengine from jsp form

我正在尝试保存通过用户从 JSP 中创建并由 java servlet 接收的前端获取的日期和时间。 以下是我的 JSP:

的代码
<form role="form" action="/abc" method="post">

            <div class="form-group">
                <div class="input-group">

                    <input type="hidden" name="div_bvg" type="number" class="form-control" value="0" required/>
                </div>
            </div>

            Enter the Details: 
                <div class="input-group">
                    <span class="input-group-addon">End Date</span>
                    <input name="div_enddate" type="datetime-local"  class="form-control" required/>
                </div>

            </div>

            <a href="/dashboard" class="btn btn-default">Cancel</a>
            <button type="submit" class="btn btn-success">Save</button>

        </form> 

以下是通过我的servlet接收

 DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
 String sdate = request.getParameter("div_enddate").trim();
 DateTime enddate = new DateTime(edate);
 Entity election = new Entity("example");
 election.setProperty("EndTime", enddate);


Transaction txn = datastore.beginTransaction();
datastore.put(txn,election);
txn.commit();

if (txn.isActive()) { //if trnasaction is still active (which means not committed properly then it rollbacks)
    txn.rollback();
  }

以下是我在提交时遇到的错误

java.lang.NumberFormatException: Invalid date/time format: 2020-01-01T01:00

我不清楚你是如何在前端花费时间的,但你将无效的时间格式传递给 Java。

您在前端使用的是这种格式:

yyyy-MM-dd'T'HH:mm

Java 期望此格式:

yyyy-MM-dd HH:mm:ss.SSS

您有 2 个选择

  1. 您可以在前端转换时间格式以匹配 java 日期格式。建议这样做,因为您可以从后端移除工作负载。
  2. 将后端中的日期解析为字符串,然后删除 "T",然后将其用作日期,即

Java时间格式转换:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
SimpleDateFormat output = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = sdf.parse(time);
String formattedTime = output.format(d);