【问题标题】:java.sql.Time : insert current time correctlyjava.sql.Time : 正确插入当前时间
【发布时间】:2017-06-21 12:52:59
【问题描述】:

我正在尝试将当前时间插入到 java.sql.Time 字段中,但我得到了这个异常:

default message [Failed to convert property value of type 'java.lang.String' to required type 'java.sql.Time' for property 'entryDate'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.sql.Time] for value ''; nested exception is java.lang.IllegalArgumentException]

这是代码:

firm.setEntryDate(Time.valueOf(java.time.LocalDate.now().toString()));

谁能告诉我我做错了什么?谢谢!

更新:Firm.class

@JsonProperty("webshow")
private String webShow;
private Time entryDate;

public String getWebShow () {
    return webShow;
}

public void setWebShow (String webShow) {
    this.webShow = webShow;
}

public Time getEntryDate () {
    return entryDate;
}

public void setEntryDate (Time entryDate) {
    this.entryDate = entryDate;
}

完全例外:

 Resolved exception caused by Handler execution: org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'firm' on field 'entryDate': rejected value []; codes [typeMismatch.firm.entryDate,typeMismatch.entryDate,typeMismatch.java.sql.Time,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [firm.entryDate,entryDate]; arguments []; default message [entryDate]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.sql.Time' for property 'entryDate'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.sql.Time] for value ''; nested exception is java.lang.IllegalArgumentException]

【问题讨论】:

  • 尝试删除.toString()并查看
  • @niceman firm.setEntryDate(Time.valueOf(java.time.LocalTime.now())); 我不得不将其更改为 LocalTime 而不是 LocalDate
  • @niceman 它在 Time.valueOf 里面,需要一个字符串
  • 你是不是真的是抛出的那条线,而不是firm.setEntryDate里面的东西?
  • 我认为这与您的二传手没有任何关系。似乎 spring 正在尝试转换某些东西。它在转换什么?

标签: java spring java-time


【解决方案1】:

试试这个:

firm.setEntryDate(java.sql.Time.valueOf(LocalTime.now()));

【讨论】:

    【解决方案2】:

    使用时间戳

    私人时间戳入口日期;

    【讨论】:

    • 时间戳?只有 TIMESTAMP(oracle 特定)和 Timestamp。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-14
    • 1970-01-01
    相关资源
    最近更新 更多