• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Date & Time

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Donald Wynn
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
"To do good, you actually have to do something." -- Yvon Chouinard
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic