java 带有时间戳和休眠的日期格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39877923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Date format with Timestamp and Hibernate
提问by Héctor
I have an entity with a Timestamp field corresponding to a DATE column in the Oracle database.
我有一个实体,其时间戳字段对应于 Oracle 数据库中的 DATE 列。
@Entity
public class Order {
private Tiemstamp purchaseDate;
//more fields...
}
When I insert a row, DATE format in the database is "dd/MM/yyyy hh:mm:ss", but I want it to be just "dd/MM/yyyy".
当我插入一行时,数据库中的 DATE 格式是“dd/MM/yyyy hh:mm:ss”,但我希望它只是“dd/MM/yyyy”。
How can I define the format?
如何定义格式?
回答by cн?dk
To ignore time
in a Date
attribute in Java
and Hibernate
, declare your attribute as java.util.Date
and either use the annotation @Type(type="date")
along with it or use the @Temporal(TemporalType.DATE)
annotation with it.
要在andtime
中的Date
属性中忽略,请将您的属性声明为,然后将注释与它一起使用或将注释与它一起使用。Java
Hibernate
java.util.Date
@Type(type="date")
@Temporal(TemporalType.DATE)
Here's what you need:
这是您需要的:
@Column
@Type(type="date")
private Date purchaseDate;
@Column
@Temporal(TemporalType.DATE)
private Date purchaseDate;
Because Timestamp
is designed to hold both date
and time
, whereas Date
holds only the date
.
因为Timestamp
旨在同时容纳date
和time
,而Date
只容纳date
。
Please refer to HIBERNATE DATE VS TIMESTAMPArticlefor further details.
有关详细信息,请参阅HIBERNATE DATE VS TIMESTAMP文章。
回答by Pastor
Despite the Oracle data type is called Date, it always stores datetime. The Oracle database does not have a data type that is unique to date without the time. In Java, instead of using the Timestamp use java.sql.Date. Do not worry about it, the Hibernete makes this treatment a safe and transparent manner.
尽管 Oracle 数据类型称为日期,但它始终存储日期时间。Oracle 数据库不具有迄今为止唯一的没有时间的数据类型。在 Java 中,不使用时间戳,而是使用 java.sql.Date。别担心,Hibernete 使这种治疗成为一种安全透明的方式。