Héctor Valls
Héctor Valls

Reputation: 26084

Date format with Timestamp and Hibernate

I have an entity with a Timestamp field corresponding to a DATE column in the Oracle database.

@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".

How can I define the format?

Upvotes: 2

Views: 31734

Answers (2)

cнŝdk
cнŝdk

Reputation: 32175

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.

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.

Please refer to HIBERNATE DATE VS TIMESTAMP Article for further details.

Upvotes: 6

Pastor
Pastor

Reputation: 318

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.

Upvotes: 1

Related Questions