Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

string to timestamp

 
Klament J. Kruoghst
Ranch Hand
Posts: 37
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20764
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic