开发者

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:

  1. in the method nullSafeGet convert the date object from the database into a string, for example using SimpleDateFormat

  2. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜