【问题标题】:unable to pass datetime to google appengine from jsp form无法从 jsp 表单将日期时间传递给 google appengine
【发布时间】:2020-05-13 09:12:11
【问题描述】:

我正在尝试通过用户从在 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

【问题讨论】:

    标签: jsp google-app-engine servlets


    【解决方案1】:

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

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

    yyyy-MM-dd'T'HH:mm
    

    Java 期望这种格式:

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

    你有两个选择

    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);
    

    【讨论】:

    • 谢谢,发现问题我有不同的日期格式并给出了不同的格式。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-08
    • 2011-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多