HIBERNATE:不想保存毫秒 [英] HIBERNATE : Don't want to save milliseconds
问题描述
我遇到了一个小问题,我在网上找不到合适的解决方案,因为我的问题对搜索引擎来说有点棘手。
有很多有关休眠节省毫秒的主题。但我的问题是其他的。
事实上,我有一个数据库,它保存了我的日期,如下所示:
2014-03-20 10:58:09
我使用 Hibernate 取回日期,并将其显示在网页上。但Hibernate检索的不止于此:它也检索0毫秒,如下所示:
2014-03-20 10:58: 09.0
很多人似乎都有这个问题,但在我的情况下,我
感谢您的帮助! p>
编辑和解决方案:
好的,我使用了一些小技巧。
在我使用Hibernate的特定对象中,我有这个方法:
public Date getModificationDate(){
return modificationDate;
}
我只是简单地创建另一个方法:
private static final SimpleDateFormat FMT = new SimpleDateFormat(dd-MM-yyyy HH:mm:ss);
public String getModificationDateLabel(){
if(modificationDate!= null){
return FMT.format(modificationDate);
}
返回null;
$ b因此,当我显示在我的网页中时(我使用Velocity Template) #foreach($ object中的$ object)
$ !{object.modificationDateLabel}
#end
SimpleDateFormat允许我删除.0 ,并且通过创建一个新的方法,我不会打扰使用Hibernate获取日期的行为。
感谢您的时间!
解决方案我没有看到返回日期的问题,因为2014-03-20 10:58:09.0等于2014-03-20 10时58分09\" 秒。你可以提供具体的情况,这可能会导致问题吗?
或者使用SimpleDateFormat(yyyy-MM-dd HH:mm:ss),然后解析你的日期格式之前使用日期。
I got a little problem and I didn't find a suitable solution on the net because my question is a bit tricky for search engine.
There's a lot of topics about hibernate saving milliseconds. But my problem is something else.
In fact, I got a database, which save my date like this :
2014-03-20 10:58:09
I used Hibernate to get back my date, and display it on a web page. But Hibernate retrieve more than that : it also retrieve a 0 milliseconds, like this :
2014-03-20 10:58:09.0
Many people seems to have problem with this, but in my case, I DON'T WANT this information, I want Hibernate to retrieve the date without this .0 !
Thanks for your help !
EDIT AND SOLUTION :
Ok so I made it by using a little hack.
In my specific object using by Hibernate, I had this method :
public Date getModificationDate() {
return modificationDate;
}
I just simply create an other method :
private static final SimpleDateFormat FMT = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
public String getModificationDateLabel() {
if (modificationDate != null) {
return FMT.format(modificationDate);
}
return null;
}
So, when I display in my webpage (I use Velocity Template), I just run through my list of object an display the label :
#foreach( $object in $objects)
$!{object.modificationDateLabel}
#end
The SimpleDateFormat allow me to remove the .0, and by creating a new method, I don't disturb the behavior of getting a Date with Hibernate.
Thanks for your time !
解决方案 I don't see a problem with the date returned as "2014-03-20 10:58:09.0" is equal to "2014-03-20 10:58:09". Can you provide specific scenario where this can result in issue?
Or use SimpleDateFormat("yyyy-MM-dd HH:mm:ss") then parse your date in this format before using the date.
这篇关于HIBERNATE:不想保存毫秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!