joptionpane未显示何时检测到无效的日期
我正在尝试检查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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论