Question

In: Computer Science

If I want to show an exception in JAVA GUI that checks if the date format...

If I want to show an exception in JAVA GUI that checks if the date format is correct or the date doesn't exist, and if no input is put in how do I do that. For example if the date format should be 01-11-2020, and user inputs 11/01/2020 inside the jTextField it will show an error exception message that format is wrong, and if the user inputs an invalid date, it will print that the date is invlaid, and if the user puts no value inside the text box, it willl print an exception message that there needs to be a value.

Please help.

Solutions

Expert Solution

A date is valid if it satisfies following constraints.

  1. It should be in correct format. For example - dd/MM/yyyy.
  2. It exist in reality. For example - 30/02/2015 is not exist so it is invalid.

For validating first constraint we are using Regular expressions and for second constraint setLenient() method of DateFormat class.

  • By default setLenient() is true. Means, it won't check whether date exist in reality or not.
  • To make it eligible for validating real dates we have to turn it off.

For example:

private static boolean isValidDate(String input) {
String formatString = "MM/dd/yyyy";

try {
SimpleDateFormat format = new SimpleDateFormat(formatString);
format.setLenient(false);
format.parse(input);
} catch (ParseException e) {
return false;
} catch (IllegalArgumentException e) {
return false;
}

return true;
}

public static void main(String[] args){
System.out.println(isValidDate("45/23/234")); // false
System.out.println(isValidDate("12/12/2111")); // true
}

The output will be false for first date in main function.

The output will be true for second date in main function.


Related Solutions

If I want to show an exception in JAVA GUI that checks if the date format...
If I want to show an exception in JAVA GUI that checks if the date format is correct or the date doesn't exist, and if no input is put in how do I do that. For example if the date format should be 01-11-2020, and user inputs 11/01/2020 inside the jTextField it will show an error exception message that format is wrong, and if the user inputs an invalid date, it will print that the date is invlaid, and if...
Can someone show me how this is supposed to look in a table format. I want...
Can someone show me how this is supposed to look in a table format. I want to double check that I'm formatting and doing the numbers properly. Thank you! Timber Construction constructs furniture.  They’ve decided they need to layout out their budgets for the first Quarter of 2019 to see if they will make a profit and have cash for a future expansion that will cost $400,000. They always must keep $100,000 minimum in the checking account every month.  (Assume the beginning...
I need a full java code. And I need it in GUI With the mathematics you...
I need a full java code. And I need it in GUI With the mathematics you have studied so far in your education you have worked with polynomials. Polynomials are used to describe curves of various types; people use them in the real world to graph curves. For example, roller coaster designers may use polynomials to describe the curves in their rides. Polynomials appear in many areas of mathematics and science. Write a program which finds an approximate solution to...
Java GPA calculator. Apply GUI, methods, exception handling, classes, objects, inheritance etc. The program should prompt...
Java GPA calculator. Apply GUI, methods, exception handling, classes, objects, inheritance etc. The program should prompt user for input. When done it should give an option to compute another GPA or exit.
Write a Java console application that reads a string for a date in the U.S. format...
Write a Java console application that reads a string for a date in the U.S. format MM/DD/YYYY and displays it in the European format DD.MM.YYYY  For example, if the input is 02/08/2017, your program should output 08.02.2017
Draw R-type format and show an example in MIPS instructions Draw I-type format and show an...
Draw R-type format and show an example in MIPS instructions Draw I-type format and show an example in MIPS instructions Draw J-type format and show an example in MIPS instructions
Create a java program that: - Has a class that defines an exception -Have that exception...
Create a java program that: - Has a class that defines an exception -Have that exception throw(n) in one method, and be caught and handled in another one. -Has a program that always continues even if incorrect data is entered by the user -has a minimum of 2 classes in it
I have a table of bike riders in the following format: Bike_number, Start_date(date and time), End_date(date,...
I have a table of bike riders in the following format: Bike_number, Start_date(date and time), End_date(date, time) I also have a table of temperatures per hour Using sql: how do I calculate Count/min/max/average of riders as compared to the temperature at the hour
I WANT TO IMPLEMENT THIS IN JAVA PLEASE I want to create a small user input...
I WANT TO IMPLEMENT THIS IN JAVA PLEASE I want to create a small user input system for a university student, where they put the season and year of when they started their uni course. For example the system will ask "What year did you start your degree?", the user will input "Autumn/2022" as a string. Now from a string format as shown, it should take that user input and calculate for example +2 or +3 years to the date....
I WANT TO IMPLEMENT THIS IN JAVA PLEASE I want to create a small user input...
I WANT TO IMPLEMENT THIS IN JAVA PLEASE I want to create a small user input system for a university student, where they put the season and year of when they started their uni course. For example the system will ask "What year did you start your degree?", the user will input "Autumn/2022" as a string. Now from a string format as shown, it should take that user input and calculate for example +2 or +3 years to the date....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT