Advertisement
irmantas_radavicius

Untitled

Mar 5th, 2022
759
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.95 KB | None | 0 0
  1. import java.util.*;
  2.  
  3.  
  4. public class Sandbox {
  5.  
  6.    
  7.     public static void main(String args[]) {       
  8.         try {      
  9.             Scanner sc = new Scanner(System.in);
  10.             System.out.println("Please enter a date (YYYY-MM-DD): ");      
  11.            
  12.             String str = sc.nextLine();
  13.            
  14.             if(str.length() == 10){                        
  15.                 boolean hasDash = str.charAt(4) == '-' && str.charAt(7) == '-';
  16.                 int count = 0;
  17.                 for(int i = 0; i < 10; ++i){
  18.                     if (Character.isDigit(str.charAt(i))){
  19.                         ++count;
  20.                     }
  21.                 }
  22.                 boolean hasDigits = (count == 8);
  23.                
  24.                 if (hasDash && hasDigits){
  25.                
  26.                     int year = Integer.parseInt(str.substring(0, 4));
  27.                     int month = Integer.parseInt(str.substring(5, 7));
  28.                     int day = Integer.parseInt(str.substring(8, 10));          
  29.            
  30.                     int [] monthSizes = {
  31.                         31, 28, 31, 30, 31, 30,
  32.                         31, 31, 30, 31, 30, 31,            
  33.                     };
  34.                    
  35.                     boolean isProperDay = false;                   
  36.                     boolean isProperMonth = true;                  
  37.                    
  38.                     if(month < 1 || month > 12){                           
  39.                         isProperMonth = false;
  40.                         System.out.println("Invalid month");
  41.                     } else {                           
  42.                         if (month != 2){
  43.                             isProperDay = (day > 0) && (day <= monthSizes[month-1]);
  44.                         } else {
  45.                             boolean isLeap = true;
  46.                             if (year % 4 != 0){
  47.                                 isLeap = false;
  48.                             } else if (year % 100 != 0){
  49.                                 isLeap = true;
  50.                             } else if (year % 400 != 0){
  51.                                 isLeap = false;
  52.                             } else {
  53.                                 isLeap = true;
  54.                             }
  55.                             isProperDay = (day > 0) && (day <= (isLeap ? 29 : 28));
  56.                         }
  57.                         if (isProperDay){
  58.                             System.out.println("The date is perfect!");
  59.                         } else {
  60.                             System.out.println("Invalid day");
  61.                         }
  62.                     }
  63.                 } else {
  64.                     System.out.println("Error, invalid date format");
  65.                 }
  66.                
  67.             } else {
  68.                 System.out.println("Error, wrong length");
  69.             }
  70.            
  71.        
  72.             sc.close();            
  73.  
  74.         } catch(Exception e){
  75.             System.out.println(e);         
  76.             System.out.println("Unexpected error, sorry!");
  77.         }  
  78.    
  79.        
  80.     }
  81.    
  82. }
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement