Advertisement
wadkat

assignment 1

Nov 19th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. package q1;
  2. import javax.swing.*;
  3. public class Q1
  4. {
  5. public static void main (String[] args)
  6. {
  7. int day,month,year;
  8. String input = JOptionPane.showInputDialog("Enter date (mm/dd/yyyy)");//input prompt
  9. String text = "";
  10. if (input.length() == 10 && input.substring(2,3).equals("/") && input.substring(5,6).equals("/"))//format check
  11. {
  12. month = Integer.parseInt(input.substring(0,2));//convert to int
  13. day = Integer.parseInt(input.substring(3,5));
  14. year = Integer.parseInt(input.substring(6,10));
  15.  
  16. if (year >= 1000 && year < 10000)//year has 4 characters
  17. {
  18. if (month == 2)
  19. //leap year february check
  20. {
  21. if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
  22. //leap year verification
  23.  
  24. {
  25. if (day < 1 || day > 29)
  26. //leap years has 29 days in february
  27. {
  28. text= "The day of leap year inputed: "+day+" is invalid, should between 1 to 29";
  29.  
  30. }
  31. else
  32. {
  33. text= "The Date inputed: "+input+" is valid.";
  34. }
  35. }
  36. else
  37. {
  38. if (day < 1 || day > 28)
  39. //non leap years have 28 days february
  40. {
  41. text= "The day of not leap year inputed: "+day+" is invalid, should between 1 to 28";
  42.  
  43. }
  44. else
  45. {
  46. text= "The Date inputed: "+input+" is valid.";
  47. }
  48. }
  49. }
  50. else if (month > 0 && month < 13 && month != 2)
  51. {
  52. if (month==4||month==6||month==9||month==11)
  53. {
  54. if (day < 1 || day > 30)
  55. {
  56. text= "The day of month inputed: "+day+" should not larger than 30, the month of "+month+" only have 30 days";
  57.  
  58. }
  59. else
  60. {
  61. text= "The Date inputed: "+input+" is valid.";
  62. }
  63. }
  64. else
  65. {
  66. if (day < 1 || day > 31)
  67. {
  68. text= "The day of month inputed: "+day+" should not larger than 31";
  69.  
  70. }
  71. else
  72. {
  73. text= "The Date inputed: "+input+" is valid.";
  74. }
  75. }
  76. }
  77. else
  78. {
  79. text= "The month inputed: "+month+" is invalid, should between 1 to 12";
  80.  
  81. }
  82. }
  83. else
  84. {
  85. text= "Invalid year value "+year+"";
  86.  
  87. }
  88. }
  89. else
  90. {
  91. text= "Invalid Date format "+input+" eg: 12/31/2018";
  92.  
  93. }
  94. JOptionPane.showMessageDialog(null, text);
  95. System.exit(0);
  96. }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement