This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do i convert a Date object into a TimeStamp object

 
Rasmeet Devji
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
I got to store the current date and time in a Timestamp variable, i could retrieve the current date and time using
Calendar cal = Calendar.getInstance(TimeZone.getDefault());
cal.getTime();
but the method of getTime() returns a date object where as i need a Timestamp object, now how do i convert a date object into a Timestamp object??
Please try and reply on this one!
Regards!
 
Michael Hildner
Ranch Hand
Posts: 297
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe something like:
 
Keith Cochran
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cal.getTime() returns a Date object, so just call getTime() from
the date object to get a long representing the millisecond time.
Then, pass this to the constructor of the Timestamp:
import java.util.Date;
import java.util.Calendar;
import java.sql.Timestamp;
import java.util.TimeZone;
public class DateTest {
public static void main(String [] s) {
Calendar cal = Calendar.getInstance(TimeZone.getDefault());
Timestamp tstamp = new Timestamp(cal.getTime().getTime());
System.out.println(tstamp.toString() + "\n");
}
}
Good Luck,
Keith
 
Anthony Smith
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you go from a timestamp to a date? How do you do that?
 
Tim Morrow
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can create a date from a timestamp in the same manner:

A java.sql.Timestamp IS a java.util.Date (it extends it) but it is quite dangerous to use them interchangably since equality is not symmetric.
You can do this:

But the following fails:

T
[ September 24, 2003: Message edited by: Tim Morrow ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic