环境:
- hibernate
- MySql 5.6(日期为 DATETIME 类型)
- Spring 3.2
- Java(日期类型为 java.util.Date)
- jdbc(mysql-connector-java 5.1.27)
当我从数据库中恢复日期值并将其打印在 html 页面中时,我得到 这个值[2014-06-13 11:39:18.0],结束点为零。
我可以在将其打印到 html 中之前进行格式化,但我不明白为什么将 .0 附加到日期。
当我通过网络编辑寄存器并更新时,格式为“yyyy-MM-dd HH:mm:ss”的初始化绑定(bind)器出现错误
...on field 'date': rejected value [2014-06-13 11:39:18.0];
codes [typeMismatch.action.date,typeMismatch.date,typeMismatch.java.util.Date,typeMismatch];
arguments [org.springframework.context.support.DefaultMessageSourceResolvable:
codes [action.date,date]; arguments [];
default message [date]];
default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'date';
nested exception is org.springframework.core.convert.ConversionFailedException:
Failed to convert from type java.lang.String to type @javax.persistence.Column java.util.Date for value '2014-06-13 11:39:18.0';
nested exception is java.lang.IllegalArgumentException]]
我找到了这个MySql Bug这似乎也是一个问题。
另一个类似的案例,使用旧的 jdbc mysql 连接器,more info .
最佳答案
使用SimpleDateFormat在保留值之前将 String
转换为 Date
。
在您的实体中注释日期字段:
@Entity
public class MyClass{
//Id and other fields
@Temporal(TemporalType.DATE)
@Column(name="DATE_FIELD")
private Date mydate;
}
DATE_FIELD
将包含以下格式的值:2014-07-16
或者您可以使用:
@Temporal(TemporalType.TIMESTAMP)
@Column(name="DATE_FIELD")
private Date mydate;
DATE_FIELD
将包含以下格式的值:2014-07-16 11:30:39
关于java - 如何删除附加的 .0 到日期值?前任。 [2014-06-13 11 :39:18. 0],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24788101/