gpt4 book ai didi

java - 我在 netbeans 中遇到 jcalender 代码错误

转载 作者:行者123 更新时间:2023-12-02 09:41:02 25 4
gpt4 key购买 nike

我正在使用 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);

最佳答案

日期和日历不是兼容的类型。您必须在它们之间进行转换:

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

还要确保使用正确的 Java 日期类型:java.util.Date 而不是 java.sql.Date

关于java - 我在 netbeans 中遇到 jcalender 代码错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57059701/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com