【问题标题】:How to create database timestamp input in HTML如何在 HTML 中创建数据库时间戳输入
【发布时间】:2018-11-21 12:44:55
【问题描述】:

所以我有这个支付历史数据库,它的属性之一是时间戳类型的时间 (YYYY-MM-DD HH:MM:SS)。我需要制作表格以添加新的付款历史记录。我的控制器将自动捕获付款历史对象并将其添加到数据库中。但由于我在 HTML 中的时间格式(我选择本地日期时间)与 DB 中的时间格式不匹配,我无法继续添加我的新付款历史记录。

这是我的错误:

Field error in object 'paymentHistoryModel' on field 'time': rejected value 
[2019-02-03T15:02]; codes 
[typeMismatch.paymentHistoryModel.time,typeMismatch.time,
typeMismatch.java.sql.Timestamp,typeMismatch]; arguments 
[org.springframework.context.support.DefaultMessageSourceResolvable: codes 
[paymentHistoryModel.time,time]; arguments []; default message [time]]; 
default message [Failed to convert property value of type 'java.lang.String' to 
required type 'java.sql.Timestamp' for property 'time'; nested exception is 
org.springframework.core.convert.ConversionFailedException: Failed to convert 
from type [java.lang.String] to type [@javax.validation.constraints.NotNull 
@javax.persistence.Column java.sql.Timestamp] for value '2019-02-03T15:02'; 
nested exception is java.lang.IllegalArgumentException: Timestamp format must 
be yyyy-mm-dd hh:mm:ss[.fffffffff]]

有没有办法在我的数据库中创建具有精确格式(如时间戳)的 HTML 输入?谢谢!!

【问题讨论】:

  • 您需要自己解析字符串,即在您的代码中。如果没有更多信息,尤其是一些代码(请记住,它应该很短,最好是 minimal reproducible example),很难说出更多信息。
  • 欢迎来到 SO!根据错误消息,您输入的时间戳是“2019-02-03T15:02”,这当然与您的格式“YYYY-MM-DD HH:MM:SS”不匹配。

标签: java html database input


【解决方案1】:

您可以使用java.text.SimpleDateFormat 更改日期时间格式。

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm"),
        sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

Date parse = sdf.parse("2019-02-03T15:02".replace("T", " "));
String dateTime = sdf2.format(parse);

System.out.println(dateTime);

输出

2019-02-03 15:02:00

详细了解Date and Time Patterns

【讨论】:

    猜你喜欢
    • 2022-01-01
    • 2015-09-14
    • 2014-02-02
    • 1970-01-01
    • 1970-01-01
    • 2021-03-02
    • 2018-05-30
    • 2010-12-02
    • 1970-01-01
    相关资源
    最近更新 更多