我正在尝试以不同的颜色在JCalendar中设置特定日期,具体取决于数据库中是否对该日期有计划,该日期在数据库中存储为“ yyyy-MM-dd”在stackOverflow上也有类似的帖子,但我无法使其正常工作。
我不确定“ component [day] .setBackground(Color.green)”的工作方式,以及如何将其设置为仅在数据库中为其计划了日期的日期
public void kalender() {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, 1);
int offset = cal.get(Calendar.DAY_OF_WEEK);
int mon = kalender.getMonthChooser().getMonth() + 1;
int yr = kalender.getYearChooser().getYear();
JPanel jPanel = kalender.getDayChooser().getDayPanel();
Component component[] = jPanel.getComponents();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String kalenderdatum = format.format(kalender.getDate());
System.out.println(kalenderdatum);
String sql2 = "SELECT DATUM FROM MOTE";
try {
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(sql2);
while (rs.next()) {
String datumet = rs.getString("DATUM");
String aret = datumet.substring(0, 4);
int year = Integer.parseInt(aret);
String manaden = datumet.substring(5,7);
int month = Integer.parseInt(manaden);
String dagen = datumet.substring(8,10);
int day = Integer.parseInt(dagen);
if(yr == year && mon == month)
{
component[day].setBackground(Color.green);
}
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
} finally {
try {
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
}