0

I'm making an airplane reservation system with java and when I run the code, it results into an error.

The error I'm receiving is:

"Incompatible types: Date cannot be converted into calendar."

And it appears at dateChooserCombo1.setSelectedDate(Calendar.getInstance().getTime()); and at dt=dateChooserCombo1.getSelectedDate(); What can I do to eliminate this errors

 try
    {
        Class .forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/a_r_s","root","");
        dateChooserCombo1.setSelectedDate(Calendar.getInstance().getTime());
        sdf= new SimpleDateFormat("dd-MM-yyyy");
    }
    catch(Exception e)
    {
     System.out.println(e.getMessage());
    }
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    final Object[] columnNames=new String[] {"Date","Flight Name","Departure Time","BC Seats Available","XC Seats Available","EC Seats Available"};
    DefaultTableModel dtm=new DefaultTableModel(columnNames,0);        
    origin=jComboBox3.getSelectedItem().toString();
    target=jComboBox4.getSelectedItem().toString();
    fclass=jComboBox1.getSelectedItem().toString();

    dt=dateChooserCombo1.getSelectedDate();      
    SimpleDateFormat sdf1= new SimpleDateFormat("yyyy-MM-dd");
    strdtver2=(String) sdf1.format(dateChooserCombo1.getSelectedDate());
    /**************************************
    //Seven days flight calendar processing
    **************************************/
    dt7=dateChooserCombo1.getSelectedDate();
    Calendar cal = Calendar.getInstance();
    cal.setTime(dt7);
    cal.add(Calendar.DATE, 7);
    dt7 = cal.getTime();
    strdtver3=(String) sdf1.format(dt7);
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • 1
    You are using terrible date-time classes that were supplanted years ago by the modern *java.time* classes with the adoption of JSR 310. – Basil Bourque Jul 16 '19 at 15:29
  • I recommend you don’t use `SimpleDateFormat`, `Date` and `Calendar`. Those classes are poorly designed and long outdated, the first in particular notoriously troublesome. Instead I believe that you just need `LocalDate` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/), instead. – Ole V.V. Jul 17 '19 at 10:25
  • I cannot reproduce your error because your code is not complete, at least there are some declarations and some imports missing. Could you [create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example), please? – Ole V.V. Jul 17 '19 at 10:26

1 Answers1

1

Date and Calendar are not compatible types. You must convert between them:

Date date= new Date();
Calendar cal = Calendar.getInstance(); 
cal.setTime(date);

Also make sure you use the correct Java Date type: java.util.Date and not java.sql.Date.

Paco Abato
  • 3,920
  • 4
  • 31
  • 54
  • Sorry, tl;dr :D Anyway, what about the second part of my answer? What type is declared your Date variable? `java.sql.Date` or `java.util.Date`? – Paco Abato Jul 16 '19 at 14:40
  • 1
    Which is the type declared for those variables? Like someone commented you should provide a complete example to allow us help you. – Paco Abato Jul 17 '19 at 12:04