• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How do i convert a Date object into a TimeStamp object

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 297
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe something like:
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you go from a timestamp to a date? How do you do that?
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic