This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Formatting Dates returned from SQL queries Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Formatting Dates returned from SQL queries" Watch "Formatting Dates returned from SQL queries" New topic
Author

Formatting Dates returned from SQL queries

Byron Bignell
Greenhorn

Joined: Sep 29, 2001
Posts: 13
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

Joined: Sep 15, 2001
Posts: 55
Your code section is empty... try posting again.
Will Sean
Greenhorn

Joined: Oct 05, 2001
Posts: 3
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

Joined: Sep 29, 2001
Posts: 13
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

Joined: Sep 29, 2001
Posts: 13
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Formatting Dates returned from SQL queries
 
Similar Threads
comparing date fields from a database
Oracle cast()+PreparedStatement
Handling Dates in JSP + Beans
why get different result in different java version
Optional parameters in EJB QL?