java - 想知道如何将 JDateChooser 与 LocalNow 进行比较

标签 java datetime date-comparison jdatechooser localdate

因此,我尝试创建一个 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 具有方法 isBeforeisAfter 用于与其他 LocalDate 对象进行比较。

旧的日期和时间类 DateSimpleDateFormat 和类似的类已被证明设计得很糟糕,尤其是 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/

相关文章:

java - Java 中的数组表达式

Java:从字符串中提取特定的 REGEXP 模式

python - 在 Pandas 中将 GroupBy 与 DateTime 结合使用 (Python)

php - 我想得到在 php 表单中填写的两个时间戳之间的差异

python - 在 Python、postgresql 中将字符串转换为 DateTime 到 UTC 格式

swift - 使用 swift 3 和 xcode 8 的 2 个日期(以周为单位)和以天为单位的日期之间的差异

javascript - 为什么 jQuery UI 的日期选择器中的这两个日期不能在 jQuery 中正确比较?

java - Collections.synchronizedList() 使用什么模式

Marklogic 使用 cts 计数旧文档 :search over FLWOR

java - Eclipse代码生成器生成mapper类