The Date problem...


Web tier: servlets, JSP, Web frameworks: The Date problem...

  1. The Date problem... (3 messages)

    hi, this is a simple question, but yet i get confused all the time and i need clarification.

    What like to know is what is the best way to convert a date value form (String--->Date).

    The other problem is that i did use the [Date] object in accessing my database (MS Access) with this quere:

    String quere = "SELECT * FROM Table WHERE Date BETWEEN '"+fromdate+"' AND '"+ todate+"'";

    where, fromdate & todate are both [Date] objects, but with no results...
    If any one can give me a model of how to use the [Date] object with the Database Date/Time fields.


    Threaded Messages (3)

  2. The Date problem...[ Go to top ]

    i *think* as you are using MS Access the data values need to go between #'s

    so BETWEEN #<fromdate>#
  3. The Date problem...[ Go to top ]

    SimpleDateFormat can convert String to Date or convert date to string easyly..
    if you use ms access ,the date param must be String(MM/dd/yyyy) adn
    closed by '#' and '# '
  4. The Date problem...[ Go to top ]

    For your query, use a PreparedStatement rather than a Statement object. You should also convert from java.util.Date to java.sql.Date:

    // Convert dates
    java.util.Date fromDate = ...
    java.util.Date toDate = ...
    java.sql.Date fromSQLDate = new java.sql.Date(fromDate.getTime());
    java.sql.Date toSQLDate = new java.sql.Date(toDate.getTime());

    // Use PreparedStatement
    String sql = "SELECT * FROM Table WHERE Date BETWEEN ? AND ?"
    PreparedStatement ps = connect.prepareStatement(sql);
    ps.setDate(1, fromSQLDate); // Replaces first "?"
    ps.setDate(2, toSQLDate); // Replaces second "?"
    ResultSet results = ps.executeQuery();

    One big advantage of a PreparedStatement is that it will automatically make the database-specific translations for your Date objects (and other SQL datatypes).