In: Computer Science
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.
/* 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.