Sunday, March 27, 2016

Check if given date is valid or not , using java coding



In order to check if the given date is valid or not you need to know the logic behind it.
  • During Leap year, February have 29 days.While generally February have 28 days in it.
  • April , June , September and November consists of 30 days.
  • While remaining months have 31 days in them.
We will be using only "if & else" conditions throughout the program coding.There is nothing else included in it.So let's get started.Here is the code for checking if given date is valid or not:


package labsessional;

import java.util.Scanner;
public class LabSessional
{
    public static void main( String [] args)
    {
        int day,month,year,leepYear ;
        Scanner input = new Scanner(System.in);
        System.out.print("Enter day : ");
        day=input.nextInt();
        System.out.print("Enter month: ");
        month=input.nextInt();
        System.out.print("Enter year: ");
        year=input.nextInt();
        leepYear=year%4;
       
    if(day>=1 && day<=31 && month>=1 && month<=12 && year>=1950 && year<=2099)
           
        {
            if (leepYear==0 && month==2 && day>=0 && day<=29)
                System.out.print("Your date is valid. ");
         else
         {
       
               
            if(month==4 || month==6 || month==9 || month==11 )
           
             {
                 if(day>=0 && day<=30)
                System.out.print("Your date is valid. ");
             else
                System.out.print("Your date is invalid. ");   
             }
   
            else if (month==2)
             {
                if(day>=0 && day<=28)
                    System.out.print("Your date is valid. ");
                else
                    System.out.print("Your date is invalid. ");
             }
             else if ( month==1|| month==3 || month==5 || month==7 || month==8 || month==10 || month==12)
              {
                  if(day>=0 && day<=31)
                      System.out.print("Your date is valid. ");
                  else
                      System.out.print("Your date is invalid. ");
              }
           
        }
    }
        else
            System.out.print("Your given date is invalid.");
 }
}


If you have any confusion or you found any error in our code , then please feel free to contact us . You can email us.Our email address is : k.ahmed2k19@gmail.com
If you liked it , then please share it with your friends. Keep visiting & Keep learning.

0 comments:

Post a Comment