想知道如何比较 JDateChooser 和 LocalNow

Wondering how to compare JDateChooser with LocalNow

所以我试图创建一个 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

已编辑:

阅读 Ole V.V 的 Calendar 和 SimpleDateFormatter 问题后。提到我意识到我给出的解决方案不是很好......下面是使用 LocalDate 和 DateTimeFormatter 的代码:

LocalDate ld = LocalDate.now();
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MMM dd, yyyy");
String output = ld.format(dtf);

原文:

我说不出为什么奥莱 V.V。告诉你要避免像 SimpleDateFormat 这样的 类,我对这个主题还不够了解。但是,要实现您想要做的事情,您可以使用:

Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy");
String tds = sdf.format(cal.getTime());

这应该可以为您提供当前时间和日期,并根据需要设置格式。

    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())和从 DateLocalDate 的转换的时区(即使您去ZoneId.systemDefault()。所以大家不用怀疑locale和time zone的使用,而且是你有意识地选择的。最后我尝试了更多解释性的变量名。