• Post Reply Bookmark Topic Watch Topic
  • New Topic

Converting string to util.date & then sql.date to query a table

Lijoy John
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I am trying to convert a String to a java.util.Date and then subsequently convert it into java.sql.Date to be queried in the database. Kindly let me know if there is a better approach for this .
In Servlets I have passed a method in which i am passing 2 parameters 'String from, String to':
and then based upon I want to get all the request(having 4 columns i.e req_id, name, status, create_date) in the database between those two dates. and then subsequently put in a table .
Note: In the database the date format is eg. 8/1/01 but when i click the cell(I am using a software to access the database) a dialog window comes up and shows the date as "Wednesday, August 01, 2001" . I am not sure if there is anything to do with this ?
This is the method allRequest():
public String allRequest(String from, String to) {
String sDateFormatter = "mm/dd/yy";
String fDate = from; // this is in the format 7/15/01
String toDate = to;
java.text.SimpleDateFormat formatter = new SimpleDateFormat(sDateFormatter);
ffdate = formatter.parse(fDate);// ffdate is java.util.Date object
ttdate = formatter.parse(toDate);
System.out.println(ffdate); // this is just to see the out put on the console.
System.out.println(fDate);// just printing the arguments in the method i.e 7/25/01
System.out.println(toDate); this is 8/2/01
long fSeconds = ffdate.getTime(); // as java.sql.Date object takes datatype long.
long tSeconds = ttdate.getTime();
fromdate = new java.sql.Date(fSeconds);
todate = new java.sql.Date(tSeconds);

System.out.println(fromdate); // here i am getting the output on console as '2001-01-25'
System.out.println(todate); // here i am getting the output on console as '2001-01-03'

}catch(ParseException ex){}
StringBuffer sb = new StringBuffer("");
PreparedStatement pstate= null;
Connection con = null;
String nu = null;
System.out.println("**Getting The connection in ALLREQUEST**");
con = getConnection();
if(con == null) {
System.out.println("Unable to establish DB connection!");
System.out.println("Cannot continue - Exiting");
return nu;
} else {
pstate = con.prepareStatement(query);
pstate.setDate(1, fromdate);// fromdate is java.sql. Date object
pstate.setDate(2, todate); // todate is java.sql. Date object
ResultSet rs = pstate.executeQuery();
sb.append("<TABLE>");// this is to add all the request in a table and show on browser.
sb.append("<TR>");// let me know if there is a better approach.
* Its not reading rs.next() at all but just prints the last line sb.append("</table>");
* on the console

while (rs.next()) {
int req_id = rs.getInt(1);
String domain = rs.getString(3);
int status = rs.getInt(4);
Date dt = rs.getDate(9);
String daate = dt.toString();// this is not working fine here.
sb.append("<TD>"+ req_id + "</TD><TD>" + domain + "</TD><TD>" + status + "</TD><TD>" +
daate + "</TD><TD>" + dt + "</TD>");
catch(SQLException ex) {
System.out.println("*&EXCEPTION in allRequest&*");
return sb.toString();
Kindly let me know how this problem could be solved.
Any help would be appreciated.

Jim Yingst
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please don't make duplicate posts in different forums. It wastes the time of people who don't realize that a question has already been answered. I'm closing this thread - see "Java in General - Advanced" for followups.
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!