How do i convert a Date object into a TimeStamp object
Rasmeet Devji
Greenhorn
Joined: Dec 08, 2000
Posts: 17
posted
0
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
Joined: Oct 13, 2000
Posts: 297
posted
0
Maybe something like:
Keith Cochran
Greenhorn
Joined: Feb 20, 2001
Posts: 4
posted
0
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
Joined: Sep 10, 2001
Posts: 285
posted
0
Can you go from a timestamp to a date? How do you do that?
Tim Morrow
Greenhorn
Joined: Sep 17, 2003
Posts: 11
posted
0
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 ]