2

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?

1
  • 1
    Date data types in a database do not have a format. Commented Oct 5, 2016 at 18:03

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I've tried with both and it is still storing the time. This is in server side. From client, I send a timestamp and, in the server, I build the date from that timestamp, call order.setPurchaseDate(theDate) and persist the entity.
Yes but I forgot to mention that you should change it from TIMESTAMPto DATE in Oracle.
1

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.

1 Comment

And here I thought their non-standard implementation of arrays was the second-weirdest thing in that database. Then again, it's the only thing I've tried out. This moves it down to third.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.