File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Formatting Dates returned from SQL queries

 
Byron Bignell
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI.
Ok, real green question, and this even after I've looked through the forums for how to format a date returned from SQL.
What I need is clarification really.
Is this right? Or is there a better way to do this?

Many thanks...
BTW...
I'm about 1/4 of the way through my first JSP/Bean project...converting a ColdFusion app...it's been great and I'm learning a huge amount.
Byron
[This message has been edited by Byron Bignell (edited October 05, 2001).]
 
Dave Soto
Ranch Hand
Posts: 55
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code section is empty... try posting again.
 
Will Sean
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is one way to format a date returned from sql queries:
// execute sql statement
ResultSet rs = stmt.executeQuery(queryString);
String[] hist = null;
int idx = 1;
hist[0] = "Date\Type";
// get resultset
while (rs.next()) {
Date tdate = rs.getDate("tdate");
String typetransaction = rs.getString("typetransaction");
hist[idx] = tdate + typetransaction;
idx++;
}
You can then display the hist array into a component, perhaps a JList or JTextArea.
 
Byron Bignell
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI.
I edited the code bit...
I'm having this problem formatting the date field of a SQL record set to a nice looking date in the JSP template...
It works just fine if I use an actual date...but if I try to parse the datetime from an ODBC/JDBC recordset I get an unparable string...or something equally as nasty.
In the end I just need to be able to make this:
2002-01-10 00:00:00.0
into
Jan 10 2001
in a JSP template.
Byron
 
Byron Bignell
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it! Got it!
(sound of palm smacking forhead!)
//first, don't just get the string as a string, get it as a date...
Date mydate = qryItems.getDate("TargetDate");
//after that is all gets easier...
SimpleDateFormat sdf = new SimpleDateFormat ("dd-MMM-yyyy");
String thedate = sdf.format(mydate);
out.println(thedate);
Major sigh of relief here...tome for that beer and some chips!
Byron
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic