This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes JDBC and the fly likes How do i convert a Date object into a TimeStamp object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "How do i convert a Date object into a TimeStamp object" Watch "How do i convert a Date object into a TimeStamp object" New topic
Author

How do i convert a Date object into a TimeStamp object

Rasmeet Devji
Greenhorn

Joined: Dec 08, 2000
Posts: 17
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
Maybe something like:
Keith Cochran
Greenhorn

Joined: Feb 20, 2001
Posts: 4
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
Can you go from a timestamp to a date? How do you do that?
Tim Morrow
Greenhorn

Joined: Sep 17, 2003
Posts: 11
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do i convert a Date object into a TimeStamp object