我正在用Java开发飞机预订系统,当我运行代码时,它会导致错误。
我收到的错误是:
"Incompatible types: Date cannot be converted into calendar."
它出现在dateChooserCombo1.setSelectedDate(Calendar.getInstance().getTime());
和dt=dateChooserCombo1.getSelectedDate();
上
我该怎么做才能消除此错误
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);
答案 0 :(得分:1)
日期和日历不是兼容类型。您必须在它们之间进行转换:
Date date= new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(date);
还要确保使用正确的Java日期类型:java.util.Date
而不是java.sql.Date
。