File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes string to timestamp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "string to timestamp" Watch "string to timestamp" New topic
Author

string to timestamp

Klament J. Kruoghst
Ranch Hand

Joined: Oct 24, 2008
Posts: 35
Hi there,

1)I have a string of this type of format: dd MMM yyyy.
2)I want to end up with a Timestamp of this format: yyyy-mm-dd hh:mm:ss

I've gone back and forth between string, date, formatter, timestamp and I keep getting exceptions of all kinds, can anyone post a code that would begin with and end with the above formats in those type of objects?

Thanks in advance
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

1. Create a SimpleDateFormat object which matches that format.

2. Use it to parse a String to a Date.

3. Convert that Date to a long value using a method whose name I think might be getTime().

4. Pass that long value to a Timestamp constructor.

By the way it doesn't make sense to require that a Timestamp should have any kind of format. It's just a time stamp, it doesn't have a format. If you need to produce a String representation of the Timestamp using some particular format then:

1. Create a SimpleDateFormat object which matches that format.

2. Use it to format the Timestamp to a String.

Rakesh K. Cherukuri
Ranch Hand

Joined: Jun 01, 2010
Posts: 48

Klament J. Kruoghst wrote:
2)I want to end up with a Timestamp of this format: yyyy-mm-dd hh:mm:ss


In the above line the month field 'mm' should be 'MM'. Coming to code part, hope this will help.



Warm Regards,
Rakesh
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: string to timestamp