• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Timestamp

 
Andrew Mcmurray
Ranch Hand
Posts: 188
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all

I am reading in two fields from a database, both are strings. The first string is in the format yyyymmdd(year month day) ex 20060801. The second is hhmmss(hours minutes seconds) ex. 231127. I have an object that takes a Timestamp as an argumnet to its constructor. I need to somehow manipulate these two strings into a time stamp so I can pass them to the other object. I looked at Timestamp and it looks like the deprecated way is to flat out parse both strings into the individual parts and pass them into the Timestamps constructor. I saw the new way is to pass the mili seconds into the Timestamps constructor. Any thoughts on how I can do this?

Thanks,

AMD
 
Purushoth Thambu
Ranch Hand
Posts: 425
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you just store yyyymmdd why do you need timestamp? Why Date objects aren't sufficient. Anyway the answer to your question is you need to parse the String value into date object using SimpleDateFormat and then get the long value of the date object and pass it as arg to Timestamp constructor.
 
Andrew Mcmurray
Ranch Hand
Posts: 188
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok so what do I do with the hhmmss string. How do I properly add that to the milis that I will be getting from the yyyymmdd string?

Thanks,

AMD
 
john guthrie
Ranch Hand
Posts: 124
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
look at SimpleDateFormat. this, or something near it, should work:

SimpleDateFormat parser = new SimpleDateFormat("yyyymmddhhmmss");
Date d = parser.parse(dateString + timeString);
return new Timestamp(d.getTime());
 
Andrew Mcmurray
Ranch Hand
Posts: 188
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right on John, thanks . The only thing I had to switch was the "mm" to "MM" for the month in the date.

AMD
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic