2
我跟着代码here设置Toedter的Calendar
中特定日期的颜色。我现在面临的问题是它没有突出显示正确的单元格。在我的例子中,我使用了6月14日和15日,但它突出了第8和第9。Java突出显示JCalendar单元格中的特定日期
而且我的继承人代码:
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date sdate= null;
String d = null;
for(int i =0;i<pd.size();i++){
d = pd.get(i).getDate();
try{
sdate = (Date)formatter.parse(d);
if(events.contains(sdate)){
}
else{
events.add(sdate);
System.out.println(sdate);
}
}catch(ParseException r){
System.out.println("error");
}
}
//arraylist of events
for(int i = 0; i < events.size(); i++)
{
Calendar cal1 = Calendar.getInstance();
cal1.setTime(events.get(i));
int day1 = cal1.get(Calendar.DAY_OF_MONTH);
int month1 = cal1.get(Calendar.MONTH);
int year1 = cal1.get(Calendar.YEAR);
//selected month and year on JCalendar
if(month == month1 && year == year1)
{
// Calculate the offset of the first day of the month
cal.set(Calendar.DAY_OF_MONTH,1);
int offset = cal.get(Calendar.DAY_OF_WEEK) -1;
component[day1 + offset ].setBackground(Color.blue);
}
}