samedi 25 juin 2016

Java highlighting specific dates in JCalendar cell

I followed the codes here to set the colors of a specific date in Toedter's Calendar. The problem I am facing now is that it is not highlighting the correct cell. In my example I have used 14th and 15th of June but it highlighted 8th and 9th.

Sccreenshot of my ui

And heres my code:

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

Aucun commentaire:

Enregistrer un commentaire