Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java & SQL comparing dates

Tags:

java

sql

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

like image 673
Batz Avatar asked Feb 07 '13 18:02

Batz


People also ask

What is Java used for?

Developers use Java to construct applications in laptops, data centres, game consoles, scientific supercomputers, cell phones, and other devices. Java is the world's third most popular programming language, after Python and C – according to the TIOBE index, which evaluates programming language popularity.

What exactly is Java?

Java is a programming language and computing platform first released by Sun Microsystems in 1995. It has evolved from humble beginnings to power a large share of today's digital world, by providing the reliable platform upon which many services and applications are built.

Is Java a programming or coding?

Java is a powerful general-purpose programming language. It is used to develop desktop and mobile applications, big data processing, embedded systems, and so on. According to Oracle, the company that owns Java, Java runs on 3 billion devices worldwide, which makes Java one of the most popular programming languages.

What is Java called now?

Java, also spelled Djawa or Jawa, island of Indonesia lying southeast of Malaysia and Sumatra, south of Borneo (Kalimantan), and west of Bali. Java is home to roughly half of Indonesia's population and dominates the country politically and economically.


1 Answers

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

like image 96
Bhavik Shah Avatar answered Sep 30 '22 19:09

Bhavik Shah