joptionpane未显示何时检测到无效的日期

发布于 2025-02-03 01:08:28 字数 2214 浏览 2 评论 0原文

我正在尝试检查mm/dd/yyyy格式的字符串是否是存在的有效日期。例如,2000年2月31日不存在,因此我希望我的程序将显示一个joptionpane,该计划指出这是无效的日期。每当我输入这样的日期时,我都会得到例外

线程中的异常“ awt-esventqueue-0”

它能够检测到它是无效的日期保存记录。

这是我的源代码:(我包括我的isfuture()方法,以防我的情况影响我的情况)

record.java

public void actionPerformed(ActionEvent e) 
{
    Object source = e.getSource();
    
    int monthInt = 0;
    for (int i = 0; i < month.length; i++) 
    {
        if (month[i].equals((String)cbMonth.getSelectedItem())) 
        {
            monthInt = i + 1;
            break;
        }
    }

    int dayInt = (int)cbDay.getSelectedItem();
    int yearInt = (int)cbYear.getSelectedItem();
    
    LocalDate bDay = LocalDate.of(yearInt, monthInt, dayInt);
    String dateStr = Integer.toString(monthInt) + "/" + Integer.toString(dayInt) + "/" + Integer.toString(yearInt);
    
    if (source == button)
    {
        // other if statements
        
        // else if given date is in the future
        else if (Person.isFuture(bDay) == true)
        {
            JOptionPane.showMessageDialog(fInfo, "An IllegalArgumentException Caught: Date given is in the future!", "Error Screen", JOptionPane.ERROR_MESSAGE);
        }
        
        // else if invalid date
        else if (Person.isValid(dateStr) == false)
        {
            JOptionPane.showMessageDialog(fInfo, "An IllegalArgumentException Caught: Invalid date!", "Error Screen", JOptionPane.ERROR_MESSAGE);
        }
        
        // else add record to map
    }
}

person。 Java

// isFuture
public static boolean isFuture(LocalDate date) 
{
    LocalDate curDate = LocalDate.of(2021, 12, 1);
    return curDate.isBefore(date);
}

// isValid()
public static boolean isValid(String date) 
{
    boolean valid = false;

    try {
        LocalDate.parse(date,
                DateTimeFormatter.ofPattern("M/d/uuuu")
                        .withResolverStyle(ResolverStyle.STRICT)
        );
        valid = true;

    } catch (DateTimeParseException e) {
        e.printStackTrace();
        valid = false;
    }

    return valid;
}

I am trying to check if a String in the format of MM/dd/yyyy is a valid date that exists. For example, February 31, 2000 does not exist so I expect that my program will show a JOptionPane that states it is an invalid date. Whenever I enter invalid dates like this, I get the exception

Exception in thread "AWT-EventQueue-0" java.time.DateTimeException: Invalid date 'FEBRUARY 31'

It is able to detect that it is an invalid date however the JOptionPane is not displaying when I try to click the button to save the record.

Here is my source code: (I included my isFuture() method in case it affects my case)

Record.java

public void actionPerformed(ActionEvent e) 
{
    Object source = e.getSource();
    
    int monthInt = 0;
    for (int i = 0; i < month.length; i++) 
    {
        if (month[i].equals((String)cbMonth.getSelectedItem())) 
        {
            monthInt = i + 1;
            break;
        }
    }

    int dayInt = (int)cbDay.getSelectedItem();
    int yearInt = (int)cbYear.getSelectedItem();
    
    LocalDate bDay = LocalDate.of(yearInt, monthInt, dayInt);
    String dateStr = Integer.toString(monthInt) + "/" + Integer.toString(dayInt) + "/" + Integer.toString(yearInt);
    
    if (source == button)
    {
        // other if statements
        
        // else if given date is in the future
        else if (Person.isFuture(bDay) == true)
        {
            JOptionPane.showMessageDialog(fInfo, "An IllegalArgumentException Caught: Date given is in the future!", "Error Screen", JOptionPane.ERROR_MESSAGE);
        }
        
        // else if invalid date
        else if (Person.isValid(dateStr) == false)
        {
            JOptionPane.showMessageDialog(fInfo, "An IllegalArgumentException Caught: Invalid date!", "Error Screen", JOptionPane.ERROR_MESSAGE);
        }
        
        // else add record to map
    }
}

Person.java

// isFuture
public static boolean isFuture(LocalDate date) 
{
    LocalDate curDate = LocalDate.of(2021, 12, 1);
    return curDate.isBefore(date);
}

// isValid()
public static boolean isValid(String date) 
{
    boolean valid = false;

    try {
        LocalDate.parse(date,
                DateTimeFormatter.ofPattern("M/d/uuuu")
                        .withResolverStyle(ResolverStyle.STRICT)
        );
        valid = true;

    } catch (DateTimeParseException e) {
        e.printStackTrace();
        valid = false;
    }

    return valid;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文