aspose file tools*
The moose likes Java in General and the fly likes Java 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 "Java Timestamp" Watch "Java Timestamp" New topic
Author

Java Timestamp

Andrew Mcmurray
Ranch Hand

Joined: Sep 24, 2005
Posts: 188
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

Joined: May 24, 2003
Posts: 425
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

Joined: Sep 24, 2005
Posts: 188
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

Joined: Aug 05, 2002
Posts: 124
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

Joined: Sep 24, 2005
Posts: 188
Right on John, thanks . The only thing I had to switch was the "mm" to "MM" for the month in the date.

AMD
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Java Timestamp