Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Resultset - Compare java date with SQL getDate()
    primarykey
    data
    text
    <p>Codes for getting current date:</p> <pre><code>SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd"); Date now = new Date(); String strDate = sdfDate.format(now); return strDate; </code></pre> <p>Codes to search date and return result</p> <pre><code>try { String x = cDate(); ps = conn.prepareStatement(fill); rs = ps.executeQuery(); int count = 0; while (rs.next()){ Date dDate = rs.getDate("recDate"); if (x.equals(dDate)){ System.out.println("True); break; } else { System.out.println("False"); } } } catch(Exception e){ JOptionPane.showMessageDialog(null, e); } </code></pre> <p>Codes below to search names and return result</p> <pre><code> try { string attendant = txtName.getText(); ps = conn.prepareStatement(fill); rs = ps.executeQuery(); int count = 0; while (rs.next()){ String bName = rs.getString("name"); if (attendant.equals(bName){ System.out.println("True); break; } else { System.out.println("False"); } } } catch(Exception e){ JOptionPane.showMessageDialog(null, e); } </code></pre> <p>I manged to get my 2nd code which is to search name and return result to work nicely. But as for the date search, it result false even I have the getDate() in SQL DB.</p> <p>I did a System.out.println(x) and System.out.println(dDate) to verify the format but it turn out to be correct.</p> <p>2013-01-17 2013-01-17</p> <p>which should return "True"</p> <p>Edited:</p> <pre><code> java.util.Date now = new java.util.Date(); java.sql.Date sqlDate = new java.sql.Date(now.getTime()); I replace (x.equals(dDate)) to (sqlDate == dDate) but the result still return as false, any idea? </code></pre> <p>Please help..</p> <p>Finally I got it..</p> <p>Solution</p> <pre><code> (Math.round(now.getTime() / 1000/3600/24) == Math.round(dDate.getTime() /1000/3600/24) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload