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!
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
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 ]