• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Hussein Baghdadi
clojure forum advocate
Bartender
Posts: 3479
Clojure Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15452
42
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Hussein Baghdadi
clojure forum advocate
Bartender
Posts: 3479
Clojure Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic