Farhan Syed
Farhan Syed

Reputation: 326

Date to datetime conversion in JPA

I see the value stored in Oracle DB as 19-JAN-13. When I get the result via:

List list1 = getEntityManager().createQuery(query).getResultList()

I see a value 2013-01-19 07:03:49.0 instead of 2013-01-19 00:00:00.0 or 2013-01-19 23:59:59.9.

I want to know why this is the case? Shouldn't the conversion of date to datetime default time to zero? The row was created at time 07:03:49.0 on 1st Jan 2013.

The accessors are defined as

@Column(name = "FREE_TRIAL_EXPIRY_DATE")
public Date getFreeTrialExpiryDate() {
    return freeTrialExpiryDate;
}
public void setFreeTrialExpiryDate(Date freeTrialExpiryDate) {
    this.freeTrialExpiryDate = freeTrialExpiryDate;
}

Upvotes: 1

Views: 1819

Answers (2)

Helio Santos
Helio Santos

Reputation: 6805

Actually, Oracle Date type stores the value of date and time. Your Oracle tool happens to show only the date DD-MM-YYYY value.

You just have to output the format properly in your Java application to have the desired DD-MM-YYYY presentation.

The bottom line is Java is not making up time values.

Upvotes: 2

Affe
Affe

Reputation: 47954

Oracle is always going to keep the time, that's just the way it is. Oracle's 'DATE' includes the time to the second, it is not equivalent to a timeless java.sql.Date.

If you want the field to be a time-less date, then you need to specify truncation at the application level by annotating with:

@Temporal(TemporalType.DATE)

on the entity.

Upvotes: 1

Related Questions