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

/* Java program to check if a date is valid or not */

/* import packeges and necessary classs */
import java.awt.event.*; 
import javax.swing.*; 

/* java class */
class Main extends JFrame implements ActionListener { 
        /* declare JTextField */
        static JTextField textBox; 

        /* declare  JFrame */
        static JFrame frame; 

        /* declare JButton */
        static JButton button; 

        /* declare label */
        static JLabel label; 

        public static void main(String[] args) 
        { 
                /* declare a frame */
                frame = new JFrame("Date Checker"); 

                /* create a label */
                label = new JLabel(""); 

                /* craete a button */
                button = new JButton("Check"); 

                /* create an object of class */
                Main obj = new Main(); 

                /* set actionlistener to button */
                button.addActionListener(obj); 

                /* declare a text box of size 10*/
                textBox = new JTextField(10); 

                /* create panel */
                JPanel panel = new JPanel(); 

                /* add field to panel */ 
                panel.add(textBox); 
                panel.add(button); 
                panel.add(label); 

                /* add the panel to frame */
                frame.add(panel); 

                /* set the size of frame */
                frame.setSize(300, 200); 
                
                /* show frame */
                frame.show(); 
        } 

        public void actionPerformed(ActionEvent e) {
                /* 
                        When button is pressed this method is called
                        @Param: Actionevent      
                        Return:     
                */
                /* get text from textbox */
                String s = e.getActionCommand(); 

                /* check if date is correct or not */
                if (s.equals("Check")) { 
                        /* check if textbox is empty */
                        if(textBox.getText().equals("")){
                                label.setText("There needs to be a value!");
                                System.out.println("There needs to be a value!");
                        }
                        /* check if all charcaters is valid */
                        else if(!isValidCharacters(textBox.getText())){
                                label.setText("Date is invalid!"); 
                                System.out.println("The date is invalid!");
                        }
                        /* check if mm-dd-yyyy format maintained */
                        else if(!isFormatCorrect(textBox.getText())){
                                label.setText("Format is wrong!");
                                System.out.println("The format is wrong!");
                        }                               
                        /* check if date exists */
                        else if(!isValid(textBox.getText())){
                                label.setText("The date doesn't exist!");
                                System.out.println("The date doesn't exist!");
                        }
                        /* correct date */
                        else{
                                label.setText("The date is correct!");
                                System.out.println("The date is correct!");
                        }
                } 
        } 
        
        public boolean isFormatCorrect(String dt){
                /* 
                        Method to check if format is correct or not
                        @Param: date string 
                        Return: if valid then true else false   
                */
                
                /* declare variable */
                int month, day;
                String temp = "";
                /* check for dash in between month-date and date-year*/
                if((int)dt.charAt(2) != 45 && (int)dt.charAt(5) != 45){
                        return false;
                }
                /* check if it mm-dd-yyyy format */
                /* check month */
                /*month range 01 to 12 */
                temp = "";
                temp += dt.charAt(0);
                temp += dt.charAt(1);
                month = Integer.valueOf(temp);
                if(!(month >=1 && month <= 12)){
                        return false;
                }
                
                /* check days */
                /*date range 01 to 31 */
                temp = "";
                temp += dt.charAt(3);
                temp += dt.charAt(4);
                day = Integer.valueOf(temp);
                if(!(day >=1 && day <= 31)){
                        return false;
                }
                return true;
        }
        
        public boolean isValidCharacters(String dt){
                /* 
                        Method to check if date contains valid charcaters
                        @Param: date string 
                        Return: if valid then true else false   
                */
                
                /* declare variables */
                int length, i;

                /* find length */
                length = dt.length();
                
                /* check length */
                /* valid length is 10 */
                if(length != 10)
                        return false;
                
                /* check if any illegal charcater are there */
                /* only digits are allowed */
                for(i = 0; i < length; i++){
                        if(i == 2 || i == 5)
                                continue;
                        if(!((int)dt.charAt(i) >= 48 && (int)dt.charAt(i) <= 57))
                                return false;
                }
                /* valid */
                return true;
        }       
        
        public boolean isValid(String dt){
                /* 
                        Method to check if date is valid or not
                        @Param: date string 
                        Return: if valid then true else false   
                */
                
                /* declare variable */
                int month, day, year;
                int leapyear;
                String temp;
                /*Find month and days */
                temp = "";
                temp += dt.charAt(0);
                temp += dt.charAt(1);
                month = Integer.valueOf(temp);
                temp = "";
                temp += dt.charAt(3);
                temp += dt.charAt(4);
                day = Integer.valueOf(temp);
                temp = "";
                temp += dt.charAt(6);
                temp += dt.charAt(7);
                temp += dt.charAt(8);
                temp += dt.charAt(9);
                year = Integer.valueOf(temp);
                
                /* check for month of 31 days */
                if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8
                                        || month == 10 || month == 12){
                                if(!(day >= 1 && day <= 31))
                                        return false;
                }
                
                /* check for month of 30 days */
                if(month == 4 || month == 6 || month == 9 || month == 11){
                                if(!(day >= 1 && day <= 30))
                                        return false;
                }
                
                /* check february */
                /* check leap year */
                if(month == 2){
                        if(year % 400 == 0)
                                leapyear = 1;
                        else{
                                if(year % 100 == 0)
                                        leapyear = 0;
                                else if(year % 4 == 0)
                                        leapyear = 1;
                                else
                                        leapyear = 0;
                        }
                
                
                        if( leapyear == 1){
                                if(!(day >=1 && day <= 29))
                                        return false;
                        }
                        else{
                                if(!(day >=1 && day <= 28))
                                        return false;
                        }
                }               
                /* valid return true */
                return true;
        }       
} 

___________________________________________________________________

___________________________________________________________________

___________________________________________________________________

​​​​​​​​​​​​​​

The format is wrong!

___________________________________________________________________

Note: If you have queries or confusion regarding this question, please leave a comment. I would be happy to help you. If you find it to be useful, please upvote.


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