aspose file tools*
The moose likes Java in General and the fly likes Converet from java.sql.Timestamp to java.util.Date Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Converet from java.sql.Timestamp to java.util.Date" Watch "Converet from java.sql.Timestamp to java.util.Date" New topic
Author

Converet from java.sql.Timestamp to java.util.Date

Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3476

Hi.
In our database table, we have a timestamp column.
At runtime, Hibernate cast it to java.sql.Timestamp class which it is not included in GWT JRE emulation library.
I tried this:
java.util.Date d = new java.util.Date(fetchedObject.getCreationTimestamp().getTime());
But I lost the time information.
Any walk around you aware of?
Thanks.
Taariq San
Ranch Hand

Joined: Nov 20, 2007
Posts: 192
Are you sure that the time went into the column to begin with?
Or better yet that it's in your timestamp object after selecting it?
Because getTime in Timestamp should get your the long you're looking for when constructing your Date.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

Note that class java.sql.Timestamp is a subclass of java.util.Date. So the Timestamp object that you have is already a java.util.Date object.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3476

Yes but Timestamp class contains the nano second part which java.util.Date doesn't handle it.
When I'm trying to display the converted Date object (according to my previous posted code), I got:
date info .. 00:00:00 ...
Obviously, I lost the time.
imaya Munusamy
Greenhorn

Joined: May 19, 2008
Posts: 20
Originally posted by John Todd:
Yes but Timestamp class contains the nano second part which java.util.Date doesn't handle it.
When I'm trying to display the converted Date object (according to my previous posted code), I got:
date info .. 00:00:00 ...
Obviously, I lost the time.


When you convert from timestamp to date, time will not be lost. the problem might be when you get date from database it might truncate time.

see the below eg:

the long value of timestamp, util.date, sql.date will be same. only when it is printed it will be printed in different way. but you are converting into util.date so will not loose time only nano sec. will not come

Timestamp st = new Timestamp(System.currentTimeMillis());
java.util.Date date1 = new java.util.Date(st.getTime());
java.sql.Date date2 = new java.sql.Date(st.getTime());

System.out.println(st); print date,Time, nano sec
System.out.println(date1); print date, time
System.out.println(date2); print date
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Converet from java.sql.Timestamp to java.util.Date
 
Similar Threads
how i can create a TimeStamp from a number like: 1183450890250, or how insert it ?
type casting issue
need to send java.util.Date to database with date AND time (so cannot use java.sql.Date)
Retrieving wrong date/time with Timestamp
JSTL: formatDate Vs Timestamp