Design a program which accepts your date of birth in dd mm yyyy format. Check whether the date entered is valid or not. If it is valid, display “VALID DATE”, also compute and display the day number of the year for the date of birth. If it is invalid, display “INVALID DATE” and then terminate the program.
Test your program for the given sample data and some random data.
Input:
Enter your date of birth in dd mm yyyy format
05
01
2010
Output:
VALID DATE
5
Input:
Enter your date of birth in dd mm yyyy format
03
04
2010
Output:
VALID DATE
93
Input:
Enter your date of birth in dd mm yyyy format
34
06
2010
Output:
INVALID DATE
import java.util.Scanner;
public class DateValid
{
//Declare Instance variable
int dd;
int mm;
int yyyy;
//Method to input date
public DateValid()
{
dd=3;
mm=4;
yyyy=2010;
}
void takeInput()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter your date of birth in dd mm yyyy format:");
dd= sc.nextInt();
mm= sc.nextInt();
yyyy= sc.nextInt();
}//EndMethod
//Method to check validity of date
void checkValidity()
{
int days[]={31,28,31,30,31,30,31,31,30,31,30,31};
int dateToDays=0;
//Check year is leap or not
int l=checkLeap(yyyy);
days[1]+=l;//to add 1 to feb if year is leap
if(mm<1 || mm>12 || dd<=0 || dd>days[mm-1] || yyyy<=0 )
System.out.println("INVALID DATE");
else
{
for(int i=0;i< mm-1;i++)
{
dateToDays+=days[i];
}//EndFor
dateToDays+=dd;//to add days of the current month
System.out.println("\nVALID DATE\n"+ dateToDays);
}//ENDELSE
}//ENDMETHOD
//Method to find and return year is leap or not
int checkLeap(int year)
{
if (year% 4 == 0)
{
if (year % 100 != 0)
return 1;
else if (year % 400 == 0)
return 1;
else
return 0;
}
else
return 0;
}//EndMethod
//Main method to call other methods
public static void main(String args[])
{
DateValid obj=new DateValid();
obj.takeInput();
obj.checkValidity();
}//EndMain
}//EndMethod