6 Dec 2017

write a program to validate date in java.

// program to validate date in java without using predefined
import java.util.*;
class  DateValidator
{
          public static void main(String[] args)
          {
                   String msg="";
                   System.out.println("enter date to validate in formate:dd/mm/yyyy");
                   String date=new Scanner(System.in).nextLine();
                   if(date.matches("^\\d{2}/\\d{2}/\\d{4}$"))
                   {
                                      String d[]=date.split("/");
                                      int day = Integer.parseInt(d[0]);
                int month = Integer.parseInt(d[1]);
                int year = Integer.parseInt(d[2]);
                                     
                switch (month) {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12: if(day
                case 4:
                case 6:
                case 9:
                case 11: if(day
                case 2://check for leap year or not 
                    if ((year % 100 == 0 && year % 400 == 0) || (year % 100 != 0 && year % 4 == 0)) {
                        if(day
                    } else {
                        if(day
                    }break;
                default:msg+="invalid date";
                }
            }
                   else
                   {
                             msg+="invalid date";
                   }
                   System.out.println(msg);
          }
}
Output: