title: Java highlighting specific dates in JCalendar cell
body: <p>I followed the codes <a href="https://stackoverflow.com/questions/16234388/add-specific-background-colors-to-jdaychooser-dates">here</a> to set the colors of a specific date in Toedter's <code>Calendar</code>. 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.</p>
  
  <p><img src="https://i.stack.imgur.com/zVca5.jpg" alt="Sccreenshot of my ui"></p>
  
  <p>And heres my code:</p>
  
  <pre><code>DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  
      Date sdate= null;
      String d = null;
      for(int i =0;i&lt;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 &lt; 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 &amp;&amp; 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); 
          }
      }
  </code></pre>