因此,我尝试创建一个 if else 语句,其中如果 jdatechooser 中的日期早于今天的日期,则会出现错误消息。据我所知,您可以通过 LocalDate.now() 获取今天的日期,将其格式化为字符串,然后执行 try-catch 将其解析回日期。对于 jdatechooser,您可以将其作为日期获取,将其格式化为字符串,然后执行 try-catch 将其解析回日期。唯一的问题是当您执行 jdate.before(localdate) 时会发生错误。还有另一种方法可以做到这一点吗?我看到您可以将今天的日期作为实例获取,但我对此还没有太多经验。
这是我尝试过的代码:
try {
//Checks to see if New Due Date is a date set before today's date
//Grabs today's date, converts it into string, then converts it into date
LocalDate ld = LocalDate.now();
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy");
String tds = sdf.format(ld);
Date tdd = sdf.parse(tds);
//Grabs reactivateDateChooser1, converts it into string, then converts it into date
Date rdc = reactivateDateChooser1.getDate();
SimpleDateFormat sdf1 = new SimpleDateFormat("MMM dd, yyyy");
String rdcs = sdf1.format(rdc);
Date rdcd = sdf1.parse(rdcs);
//If the new Due Date is before or on today's date, then error message will show
if(rdcd.before(tdd)){
JOptionPane.showMessageDialog(null, "Please select a date after " + tds, "Select Valid Date", JOptionPane.ERROR_MESSAGE);
}
//If the date chooser is empty, then error message will show
else if (reactivateDateChooser1.getDate() == null){
JOptionPane.showMessageDialog(null, "Please select a date", "Needs Date", JOptionPane.ERROR_MESSAGE);
}
//Otherwise, if the new date is after today's date, then the function will happen
else if (rdcd.after(tdd)){
changeStatusToActive();
statusCheck1.setText("");
refreshClassTable();
closeReactivateDialog();
}
} catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
这是我收到的错误:
java.lang.IllegalArgumentException: Cannot format given Object as a Date
最佳答案
DateTimeFormatter dateFormatter
= DateTimeFormatter.ofPattern("MMM dd, yyyy", Locale.ITALY);
//Checks to see if New Due Date is a date set before today's date
//Grabs today's date
LocalDate today = LocalDate.now();
//Grabs reactivateDateChooser1, converts it into LocalDate (if not null)
Date reactivateUtilDate = reactivateDateChooser1.getDate();
//If the date chooser is empty, then error message will show
if (reactivateUtilDate == null) {
JOptionPane.showMessageDialog(null, "Please select a date",
"Needs Date", JOptionPane.ERROR_MESSAGE);
} else {
LocalDate reactivateDate = reactivateUtilDate.toInstant()
.atZone(ZoneId.of("Asia/Singapore"))
.toLocalDate();
//If the new Due Date is before or on today's date, then error message will show
//Otherwise, if the new date is after today's date, then the function will happen
if (reactivateDate.isAfter(today)) {
changeStatusToActive();
statusCheck1.setText("");
refreshClassTable();
closeReactivateDialog();
} else {
String todayString = today.format(dateFormatter);
JOptionPane.showMessageDialog(null,
"Please select a date after " + todayString,
"Select Valid Date", JOptionPane.ERROR_MESSAGE);
}
}
与其从现代的 LocalDate
转换为过时的 Date
,不如尽可能使用现代且更好的 API。我知道您无法避免从 JDateChooser
获取 java.util.Date
,因此首先将其转换为现代 Instant
类型并进一步执行从那里进行转换以获得 LocalDate
,您可以将其与您拥有的日期进行比较。 LocalDate
具有方法 isBefore
和 isAfter
用于与其他 LocalDate
对象进行比较。
旧的日期和时间类 Date
、SimpleDateFormat
和类似的类已被证明设计得很糟糕,尤其是 SimpleDateFormat
是出了名的麻烦。只要看看 Stack Overflow 上有多少关于 SimpleDateFormat
令人惊讶和不受欢迎的行为的问题即可。所有这些都是 Oracle 在 2014 年发布 java.time 作为替代品的原因。当然,现代 API 更易于使用。我热烈推荐它。
我进一步建议您为格式化程序指定区域设置(即使您只指定 Locale.getDefault()
)以及从 Date
到 转换的时区LocalDate
(即使您选择 ZoneId.systemDefault()
)。因此,没有人需要怀疑使用了语言环境和时区,并且您是有意识地选择了它们。最后我尝试了更具解释性的变量名称。
关于java - 想知道如何将 JDateChooser 与 LocalNow 进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48505457/