• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Formatting Dates returned from SQL queries

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code section is empty... try posting again.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 will give me the powers of the gods. Not bad for a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic