0

Codes for getting current date:

SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd");
Date now = new Date();
String strDate = sdfDate.format(now);
return strDate;

Codes to search date and return result

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);
  }

Codes below to search names and return result

   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);
  }

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.

I did a System.out.println(x) and System.out.println(dDate) to verify the format but it turn out to be correct.

2013-01-17 2013-01-17

which should return "True"

Edited:

        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?

Please help..

Finally I got it..

Solution

       (Math.round(now.getTime() / 1000/3600/24) == Math.round(dDate.getTime() /1000/3600/24)
user1815586
  • 93
  • 1
  • 5
  • 12

3 Answers3

1

I think the problem is because you are trying to compare a String with a date object which is going to be false although the values are same but both are different objects.

String x = cDate();
...
Date dDate = rs.getDate("recDate");
...
if(x.equals(dDate)){

The default behavior of equals method compares whether object references are pointing to the same object and not based on the values.

Abubakkar
  • 15,488
  • 8
  • 55
  • 83
  • 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? – user1815586 Jan 16 '13 at 18:20
  • it is because your variables `sqlDate` and `now` are referring to different objects and `==` checks if both the variables are referring to same object or not, `==` doesn't check for value. – Abubakkar Jan 17 '13 at 03:15
1

At the line :

x.equals(dDate)

x is a String, and dDate is a java.sql.Date. They are not equals.

obourgain
  • 8,856
  • 6
  • 42
  • 57
1

Java Date contains time information also. Dates could differ in minutes or even seconds. To make sure both dates has exactly the same value you should print out date.getTime() which return long value of milliseconds since January 1, 1970, 00:00:00 GMT. If you wanna compare you should convert x from String to Date and then do this:

  x.getTime() == dDate.getTime()

however, if you wanna compare just year, month and day you should trunk like this:

  Math.round(x.getTime()/1000/3600/24) == Math.round(dDate.getTime()/1000/3600/24)

More about Date comparision in Java you can find here: Date Comparison using Java

Community
  • 1
  • 1
Archer
  • 5,073
  • 8
  • 50
  • 96
  • I realised (Math.round(now.getTime() / 1000/3600/24) == Math.round(dDate.getTime() /1000/3600/24) doesn't do the comparison. It just return the results of the names. e.g 17th Jan is today but results includes 16th Jan. Any idea? – user1815586 Jan 17 '13 at 15:18
  • Can't be. It should compare dates within same day. You're truncating by 1000 (milliseconds in second) 3600 (seconds in hour) 24 (hours in day) – Archer Jan 17 '13 at 19:43