【发布时间】:2018-04-11 12:18:19
【问题描述】:
我有一个日历,所以我可以通过以下方式获取日期:
Calendar cld = new Calendar();
cld.getCurrentDate();
还有:
cld.getDate();
所以我有当前日期和我在日历中选择的日期。我想做的是:
Date currentdate = cld.getCurrentDate();
Date chosendate = cld.getDate();
long math = Math.abs(currentdate.getTime() - chosendate.getTime());
long result = math / (24 * 60 * 60 * 1000);
if(result < 0)Dialog.show("Invalid Date", "Please select a valid date.", "OK", null);
不确定我的方法是否正确,实际上我想要一种方法来避免从日历中获取未来日期,如何在 Codename One 中做到这一点?
已编辑:
Calendar cld = new Calendar() {
protected void updateButtonDayDate(Component dayButton, int year, int currentMonth, int day) {
super.updateButtonDayDate(dayButton, year, currentMonth, currentDay);
dayButton.setEnabled(isDateValid(year, currentMonth, day));
}
};
我的 isDateValid() 方法:
import java.util.Calendar;
public class DateValidator {
public boolean isDateValid(int year, int currentMonth, int day) {
if(Calendar.MONTH < currentMonth-1)return false;
else {
return true;
}
}
}
我必须创建一个新类,因为我已经在我的另一个类中使用代号 One 中的日历,并且它不提供 Calendar.MONTH。我无法选择未来的几个月,但它仍然允许我选择未来的日子和未来的几年。我希望我可以使用原始 java.util.Date 中的 Date.after() 方法,Codename One 中的 Date 没有 after() 方法,这让我很难找到方法。有什么帮助吗?
【问题讨论】:
标签: codenameone