禁用多个日期范围jDateChooser [英] Disable multiple date ranges jDateChooser
问题描述
我想在 JCalendar 上禁用多个日期范围。我正在关注这些步骤,但我需要知道如何添加多个日期评估程序。
请帮帮我,谢谢。
I want to disable multiple date ranges on a JCalendar. I'm following these steps, but I need to know how can I add multiple date evaluators. Help me please, thanks.
更新:我不知道为什么我无法禁用我的 JCalendar 。我将把代码留在这里,以便你们可以检查它。
Update: I don't know why I can't disable the dates on my JCalendar. I will leave my code here so you guys can check it.
这是我的RangeEvaluator类,修改自此课程。
This is my RangeEvaluator class, modified from this class.
class RangeEvaluator implements IDateEvaluator {
private DateUtil dateUtil = new DateUtil();
@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) {
return dateUtil.checkDate(date);
// if the given date is in the range then is invalid
}
/**
* Sets the initial date in the range to be validated.
* @param startDate
*/
public void setStartDate(Date startDate) {
dateUtil.setMinSelectableDate(startDate);
}
/**
* @return the initial date in the range to be validated.
*/
public Date getStartDate() {
return dateUtil.getMinSelectableDate();
}
/**
* Sets the final date in the range to be validated.
* @param endDate
*/
public void setEndDate(Date endDate) {
dateUtil.setMaxSelectableDate(endDate);
}
/**
* @return the final date in the range to be validated.
*/
public Date getEndDate() {
return dateUtil.getMaxSelectableDate();
}
@Override
public String getInvalidTooltip() {
return null;
}
@Override
public Color getInvalidBackroundColor() {
return null;
}
@Override
public Color getInvalidForegroundColor() {
return null;
}
}
以下是我使用RangeEvaluator类的方法:
Here is how I am using the RangeEvaluator class:
RangeEvaluator evaluator = new RangeEvaluator();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-mm-yyyy");
evaluator.setStartDate(dateFormat.parse("11-09-2014"));
evaluator.setEndDate(dateFormat.parse("15-09-2014"));
jCalendar1.getDayChooser().addDateEvaluator(evaluator);
我错过了什么吗?
请帮助我,谢谢。
Am I missing something?. Help me please, Thanks.
推荐答案
根据您的更新,这里有两件事情发生。
Based on your update there are two things that happen here.
当您实例化 SimpleDateFormat :
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-mm-yyyy");
在此模式中,mm表示分钟而不是月。它应该是:
In this pattern "mm" refers to minutes not months. It should be:
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
第二个问题是恕我直言,因为日期选择器似乎不会应用过滤器,直到设置新的日期或由于月/年的变化,它必须重新粉刷。你可以测试它改变月份到8月然后回到9月:9月11日到9月15日的日期将被禁用。要解决这个不方便的行为,只需明确地设置当前日期:
Second problem is IMHO a bug since day chooser seems to not apply filters until a new date is set or it has to be repainted because of month/year change. You can test it changing month to August and then back to September: dates from 9/11 to 9/15 will be disabled. To solve this inconvenient behavior just set current date explicitely like this:
JCalendar calendar = new JCalendar();
calendar.getDayChooser().addDateEvaluator(evaluator);
calendar.setDate(Calendar.getInstance().getTime());
旁注:虽然这个库非常有用但它有一些bug /设计问题所以请不要犹豫,寻求帮助。
Side note: while this library is very very useful it has a couple of bug/design issues so don't hesitate to ask for help.
这篇关于禁用多个日期范围jDateChooser的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!