I have a Date type column in Oracle DB and it contains date and time for sure. But when I'm trying to get data in java application it will return date with bunch of zeros instead of real time. In code it'll be like:
SQLQuery sqlQuery = session.createSQLQuery("SELECT table.id, table.date FROM table");
List<Object[]> resultArray = sqlQuery.list();
Date date = (Date)resultArray[1];
If it was 26-feb-2010 17:59:16 in DB I'll get 26-feb-2010 00:00:00 How to get it with time?
Use the toDateString() method to remove the time from a date, e.g. new Date(date. toDateString()) . The method returns only the date portion of a Date object, so passing the result to the Date() constructor would remove the time from the date.
No time data is kept. In fact, the date is stored as milliseconds since the 1st of January 1970 00:00:00 GMT and the time part is normalized, i.e. set to zero. Basically, it's a wrapper around java. util.
Use DateUtils. truncate() to throw out all fields less significant than the specified field. When a Date is truncated to the Calendar. MONTH field, DateUtils.
As others have already stated, you need to use java.sql.Timestamp
to get the time.
However, if you use the @Temporal(TemporalType.TIMESTAMP)
annotation on a Oracle's DATE column, Hibernate will complain and throw schema validation errors. To avoid those, add a columnDefinition
like this:
@Temporal(TemporalType.TIMESTAMP)
@Column(name="EVENTTIME", columnDefinition="DATE")
private Date eventTime;
I am not an expert with Oracle, but you probably need a DateTime
column type to get a time stamp.
With that you need to use java.sql.Timestamp
JDBC type.
I had the same problem (Oracle Date type in the database).
The following mapping works for me:
<property name="timeStamp" type="java.util.Date">
<column name="TIME_STAMP"/>
</property>
I've encountered this issue in the past too. To resolve it, change your hibernate mapping from:
<property name="timeStamp" type="java.util.Date">
<column name="TIME_STAMP"/>
</property>
to
<property name="timeStamp" type="timestamp">
<column name="TIME_STAMP"/>
</property>
You can leave the data type in your Hibernate object as java.util.Date.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With