java.sql.Time:正确插入当前时间

时间:2017-06-21 12:53:00

标签: java spring java-time

我试图将当前时间插入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]

2 个答案:

答案 0 :(得分:2)

试试这个:

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

答案 1 :(得分:-1)

使用timeStamp

私人TimeStamp entryDate;