I'm using the JCalendar
datepicker and would like to know if there's a way to make only certain weekdays (e.g Monday) selectable via the JDateChooser
component?
Does anyone know how to do it?
I'm using the JCalendar
datepicker and would like to know if there's a way to make only certain weekdays (e.g Monday) selectable via the JDateChooser
component?
Does anyone know how to do it?
Below is an example of implementing this as DayOfWeekEvaluator
accepting a list of DayOfWeek
objects:
import com.toedter.calendar.IDateEvaluator;
import java.awt.Color;
import java.time.DayOfWeek;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
public class DayOfWeekEvaluator implements IDateEvaluator {
private final List<DayOfWeek> validDaysOfWeek;
public DayOfWeekEvaluator(List<DayOfWeek> validDaysOfWeek) {
this.validDaysOfWeek = validDaysOfWeek;
}
private DayOfWeek determineDayOfWeek(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
return DayOfWeek.of(convertSundayFirstToMondayFirst(dayOfWeek));
}
private int convertSundayFirstToMondayFirst(int sundayFirstValue) {
// Converts from weeks that are Sunday-Saturday (1-7)
// to weeks that are Monday-Sunday (1-7)
if (sundayFirstValue == 1) {
return 7;
}
return sundayFirstValue - 1;
}
@Override
public boolean isSpecial(Date date) {
return false;
}
@Override
public Color getSpecialForegroundColor() {
return null;
}
@Override
public Color getSpecialBackroundColor() {
return null;
}
@Override
public String getSpecialTooltip() {
return null;
}
@Override
public boolean isInvalid(Date date) {
DayOfWeek dayOfWeek = determineDayOfWeek(date);
return !validDaysOfWeek.contains(dayOfWeek);
}
@Override
public Color getInvalidForegroundColor() {
return null;
}
@Override
public Color getInvalidBackroundColor() {
return null;
}
@Override
public String getInvalidTooltip() {
return null;
}
}
With the logic being in the private methods utilized in isInvalid
. Example usage could for example be:
List<DayOfWeek> validDaysOfWeek = Arrays.asList(DayOfWeek.MONDAY, DayOfWeek.THURSDAY);
JCalendar c = new JCalendar();
c.getDayChooser().addDateEvaluator(new DayOfWeekEvaluator(validDaysOfWeek));
c.setCalendar(Calendar.getInstance());
To see a full example of this (with a main method), see this example gist.