Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package q1;
- import javax.swing.*;
- public class Q1
- {
- public static void main (String[] args)
- {
- int day,month,year;
- String input = JOptionPane.showInputDialog("Enter date (mm/dd/yyyy)");//input prompt
- String text = "";
- if (input.length() == 10 && input.substring(2,3).equals("/") && input.substring(5,6).equals("/"))//format check
- {
- month = Integer.parseInt(input.substring(0,2));//convert to int
- day = Integer.parseInt(input.substring(3,5));
- year = Integer.parseInt(input.substring(6,10));
- if (year >= 1000 && year < 10000)//year has 4 characters
- {
- if (month == 2)
- //leap year february check
- {
- if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
- //leap year verification
- {
- if (day < 1 || day > 29)
- //leap years has 29 days in february
- {
- text= "The day of leap year inputed: "+day+" is invalid, should between 1 to 29";
- }
- else
- {
- text= "The Date inputed: "+input+" is valid.";
- }
- }
- else
- {
- if (day < 1 || day > 28)
- //non leap years have 28 days february
- {
- text= "The day of not leap year inputed: "+day+" is invalid, should between 1 to 28";
- }
- else
- {
- text= "The Date inputed: "+input+" is valid.";
- }
- }
- }
- else if (month > 0 && month < 13 && month != 2)
- {
- if (month==4||month==6||month==9||month==11)
- {
- if (day < 1 || day > 30)
- {
- text= "The day of month inputed: "+day+" should not larger than 30, the month of "+month+" only have 30 days";
- }
- else
- {
- text= "The Date inputed: "+input+" is valid.";
- }
- }
- else
- {
- if (day < 1 || day > 31)
- {
- text= "The day of month inputed: "+day+" should not larger than 31";
- }
- else
- {
- text= "The Date inputed: "+input+" is valid.";
- }
- }
- }
- else
- {
- text= "The month inputed: "+month+" is invalid, should between 1 to 12";
- }
- }
- else
- {
- text= "Invalid year value "+year+"";
- }
- }
- else
- {
- text= "Invalid Date format "+input+" eg: 12/31/2018";
- }
- JOptionPane.showMessageDialog(null, text);
- System.exit(0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement