0

I am writing this section of code to compare a query to the database with the current date in order to tell if a student is already checked into class. I can cast them both to string and they look identical but they are different in some way at a byte level and the if statement is still returning false. What can be done?

Also this does not work.

java.sql.Date date = new Date();

It gives an error that is why I tried changing the format to .sql.Date with the first 2 lines. It did not help but I left it in there to show what I have tried.

here is the code

public boolean checkTodaysAttendance(int ID){
    //date stuff
    java.util.Date date = new java.util.Date();
    java.sql.Date sqlDate = new java.sql.Date(date.getTime());

    //System.out.println(sqlDate);
    boolean attend = false;
    try {
        PreparedStatement statement = con.prepareStatement("SELECT Date FROM attendance WHERE ID="+ID);
        rs1 = statement.executeQuery();
        while(rs1.next()) {
            //System.out.println(rs1.getDate("Date"));
            if (rs1.getDate("Date").toString().trim(). == sqlDate.toString().trim()) { //my problem is on this line
                attend = true;                 //string don't equal at a byte level
            }
        }

    }catch(Exception ex){
        System.out.println(ex);
    }
    //System.out.println(attend);
    return attend;
}

1 Answers1

0

Change both dates to strings using simple date format and then compare.

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-hh.mm.ss");

// create a new String using the date format we want
String dateString= formatter.format(date);
Aajan
  • 927
  • 1
  • 10
  • 23