Win a copy of OCP Oracle Certified Professional Java SE 11 Developer Practice Tests this week in the OCP forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
Sheriffs:
  • Devaka Cooray
  • paul wheaton
  • Mark Herschberg
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
  • Jj Roberts
Bartenders:
  • Carey Brown
  • salvin francis
  • Piet Souris

string to timestamp

 
Ranch Hand
Posts: 37
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 26377
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 48
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
I am mighty! And this is a mighty small ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic