Java 在 JCalendar 单元格中突出显示特定日期

Java highlighting specific dates in JCalendar cell

我按照代码 here 在 Toedter 的 Calendar 中设置特定日期的颜色。我现在面临的问题是它没有突出显示正确的单元格。在我的示例中,我使用了 6 月 14 日和 15 日,但突出显示了 8 日和 9 日。

这是我的代码:

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); 
        }
    }

作为更改组件的替代方法,实施 IDateEvaluator 和 return 所需的颜色,按照建议 . It's not clear where your Calendar offset goes awry. The example below uses List::contains 来识别特殊日期。请务必清除您 add().

日历日期上的时间字段

import com.toedter.calendar.IDateEvaluator;
import com.toedter.calendar.JCalendar;
import java.awt.Color;
import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.swing.JFrame;

/**
 * @see 
 * @see 
 */
public class HighlightTest {

    private static class HighlightEvaluator implements IDateEvaluator {

        private final List<Date> list = new ArrayList<>();

        public void add(Date date) {
            list.add(date);
        }

        @Override
        public boolean isSpecial(Date date) {
            return list.contains(date);
        }

        @Override
        public Color getSpecialForegroundColor() {
            return Color.red.darker();
        }

        @Override
        public Color getSpecialBackroundColor() {
            return Color.blue;
        }

        @Override
        public String getSpecialTooltip() {
            return "Highlighted event.";
        }

        @Override
        public boolean isInvalid(Date date) {
            return false;
        }

        @Override
        public Color getInvalidForegroundColor() {
            return null;
        }

        @Override
        public Color getInvalidBackroundColor() {
            return null;
        }

        @Override
        public String getInvalidTooltip() {
            return null;
        }
    }

    private void display() {
        JFrame f = new JFrame("Highlight Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        HighlightEvaluator evaluator = new HighlightEvaluator();
        evaluator.add(createDate(14));
        evaluator.add(createDate(15));
        JCalendar jc = new JCalendar();
        jc.getDayChooser().addDateEvaluator(evaluator);
        jc.setCalendar(jc.getCalendar());
        f.add(jc);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    private Date createDate(int d) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.DAY_OF_MONTH, d);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return (c.getTime());
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new HighlightTest()::display);
    }
}