aspose file tools*
The moose likes Java in General and the fly likes Date & Time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Date & Time" Watch "Date & Time" New topic
Author

Date & Time

Donald Wynn
Greenhorn

Joined: Dec 04, 2002
Posts: 29
I want to convert a string of char. to a formatted date and time. I am getting a record a database and I want to convert that into formatted date how can I do this??? My string would look like this:
1900-01-01 00:00:00.0 and I want to convert that into Jan 1 1900..
John Lee
Ranch Hand

Joined: Aug 05, 2001
Posts: 2545
I think what you can do is to parse the string, then use switch statement to get the month, then get rid of "0" of "01" in day expression, then rearrange the order.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
SimpleDateFormat is your friend:

The only problem here is that the milliseconds field isn't correct. A value like 12.3 seconds will be translated as 12.003 seconds. (SimpleDateFormat doesn't seem to offer a code for tenths of a second, just milliseconds.) If the milliseconds don't really matter to you, this is fine - ignore it. Otherwise it's possible to correct the milliseconds like this:


"I'm not back." - Bill Harding, Twister
Donald Wynn
Greenhorn

Joined: Dec 04, 2002
Posts: 29
I'm using a ResultSet to get the data from the database and one field is a date, and when I said rs.getObject(i) it returns the date field as 1900-01-01 00:00:00.0. Milliseconds is not that important to me I just want to convert the date I get from the db to Jan 01 1900 00:00.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Oh. Well, you should probably get the date with rs.getDate(i) rather than rs.getObject(i). This will return a java.sql.Date, which is a subclass of java.util.Date - so you don't need the first DateFormat to parse the date; ResultSet does it for you directly. If you have any trouble, you might try using a getTimestamp() instead (if that's how the column is declared in the DB).
Donald Wynn
Greenhorn

Joined: Dec 04, 2002
Posts: 29
That doesn't work....because the format of the date on the db is Jan 1 1900 12:00AM. What do I need to do?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I'm not sure what you mean, Donald. Is the field declared as a Date in the database or not? Does getDate() return an object, or does it throw an exception? If it returns an object and you're just not happy with the format that's displayed when you print it, that's fine - you can still use the second SimpleDateFormat I gave you (df2) to format it differently. I'm just saying you don't need the first one (df1) to parse the date as a Date object - the ResultSet does this for you.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Date & Time