Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting date attribute from database breaks

 
Foni kumar
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi Experts ,

I am creating a code in java where I need to fetch the values of the date_of_creation and date_of_modify attributes. But both of them are in different formats in the database table like date_of_creation = '2012-04-01' and date_of_modify = '2012-03-02 10:30:21:003' here 10:30:21:003 denotes the time "003 is the milliseconds".


To fetch string values I am using the below code:
System.out.println("ID: "+rset.getString("ID"));

But when I make use of getDate() method the code breaks, can any one suggest how can I handle this situation for getting the dates in the different formats as has been mentioned above.


Thanks and regards,
deb
 
Niall Loughnane
Ranch Hand
Posts: 209
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Deb,

Hope this helps,

I presume the structure of the 2 date fields are different in the database,

1 field is a date field,
1 field is a date time field,

You may need to have a look at reformatting the database structure,

Else if thats not possible you could have a look at using SimpleDateFormat for formatting the response from the getString method call,

Hope it helps :)

Niall
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have a DATE or TIMESTAMP columns in the database, you should use ResultSet.getDate() or ResultSet.getTimestamp() methods to fetch the values. You'll obtain java.sql.Date or java.sql.Timestamp from these methods, and you can the convert these objects into java.util.Date or a Calendar instances and format/print these out.

Another possibility would be to convert the columns into a text using database function, such as TO_CHAR() in Oracle (this would inevitably be database specific code); then you could obtain the value using ResultSet.getString() and print it out. However, if you need to do more than just print/display the value (such as perform computations on it), I'd say you should use the first approach.
 
Foni kumar
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Martin Vajsar wrote:If you have a DATE or TIMESTAMP columns in the database, you should use ResultSet.getDate() or ResultSet.getTimestamp() methods to fetch the values. You'll obtain java.sql.Date or java.sql.Timestamp from these methods, and you can the convert these objects into java.util.Date or a Calendar instances and format/print these out.

Another possibility would be to convert the columns into a text using database function, such as TO_CHAR() in Oracle (this would inevitably be database specific code); then you could obtain the value using ResultSet.getString() and print it out. However, if you need to do more than just print/display the value (such as perform computations on it), I'd say you should use the first approach.




Thankyou very much Martin, my problem got solved. I followed the suggested ideas you provided. cool man! and many thanks to all others who helped in providing their valuable suggestions
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic