6

I am trying to get client arrival date and compare it with my SQL database to see if in my data base the same date exists. however i receive the following error: The operator > is undefined for the argument type(s) java.lang.String, java.lang.String

P.S I need to compare it via java not using sql query

public void makeNewReservation() throws ParseException {
    // Enter informations

    System.out.println("Date of arrivel?");
    Scanner in = new Scanner(System.in);
    String date_entree = in.next();
    System.out.println("Date of exit? dd/MM/yyyy");
    String date_sortiee = in.next();
    calculateDaysDifference(date_sortiee, date_entree);



public  void calculateDaysDifference(String date_entree, String date_sortiee) throws ParseException{


ConnectionMySQL myConnection=new ConnectionMySQL();
    Connection conSQL=myConnection.startDBConnection();
    boolean connectionOK=myConnection.checkConnection(conSQL);

    String query = ("SELECT `START_DATE`,`END_DATE` FROM `room_booking");


    //if everything is fine with the connection, i try to execute a query
    if (connectionOK){
        try{
            ResultSet mesResultats=myConnection.executeQuery(conSQL, query);

            //the while loop is just for me to check the dates
            while (mesResultats.next()) {
                System.out.println("START_DATE: "+mesResultats.getString(1)+" END_DATE : "+ mesResultats.getString(2));


                if (date_entree > mesResultats.getString(1){
                    System.out.println("cant reserve room room reserved already");


                }
            }


            // je ferme la connexion
            conSQL.close();

        }

        catch(SQLException e){
            e.printStackTrace();


        }

    }

my data base

6
  • why can't you compare in the db? Commented Feb 7, 2013 at 18:02
  • It's pretty straightforward. You have Strings. You're trying to compare them like numbers. Right here: ` if (date_entree > mesResultats.getString(1){` Either convert them, if that's what you want, or use String.Compare() Commented Feb 7, 2013 at 18:03
  • the teacher wants Java code ! :) Commented Feb 7, 2013 at 18:04
  • 1
    I'd store it as a DATE in the database, map the value into a java.util.Date, and compare that way. Commented Feb 7, 2013 at 18:07
  • the dates in my data base are stored as Date. but before I store the clients dates I must check if there is a room. then I will store them. Commented Feb 7, 2013 at 18:08

2 Answers 2

4

You need to compare 2 Dates

1) Convert the input String into Date

SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
java.util.Date d=df.format(/*date String*/);

NOTE: df.format will throw parseException if the String format does not match "yyyy-MM-dd" . I leave it upto you to make sure the date string is of the specified format. 

2)get Date from sql query

java.util.Date sqlDate=new java.util.Date(resultset.getDate().getTime());

NOTE : resultset.getDate() will give you java.sql.Date class's object.

3) Compare 2 dates

Sign up to request clarification or add additional context in comments.

Comments

3

try this logic

Date date1=new Date(df.parse(mesResultats.getString(1)));
Date date2=new Date(df.parse(mesResultats.getString(2)));

int status=date1.compareTo(date2); //compareto is a function defined for date

if status==0 print same date
if status<0 print  date1 is older then date2
if status>0 print  date1 is newer then date2

[Update after comment] DateFormat df = new SimpleDateFormat("Format of your date goes here");

1 Comment

i am receiving the error msg: Cannot make a static reference to the non-static method parse(String) from the type DateFormat

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.