time format yyyyMMddHHmmss in Hibernate 3.0
I need time in "yyyyMMddHHmmss" format for Hibernate 3.0.
<property name="delayTime" column="DELAY开发者_JAVA技巧TIME" type=""/>
what i need to specify type to reflect time in above format?
Sounds like you want a org.hibernate.usertype.UserType
Create a class that implements that interface then:
in the method nullSafeGet convert the date object from the database into a string, for example using SimpleDateFormat
in the method nullSafeSet do the reverse. Convert your string into a date object before saving into the database.
Actually the above answer seems like overkill. Why not just keep the date as a java.util.Date in the hibernate object. Then you can have methods like:
private Date date;
public String getDate() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YOUR_FORMAT);
return simpleDateFormat.format(date);
}
public void setDate() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YOUR_FORMAT);
this.date = simpleDateFormat.parse(date);
}
Hibernate can use the private date. The rest of your code can use the String.
精彩评论