In this we are taking date, month and year with three different variable and then check whether the date is valid or not. The below program shows how to check whether the data value is valid or not.
- Example
import java.util.*; class GetBirthDate { //get the birthdate of his or her public static void main(String args[]) { int dt,month,year,leap=0,noOfDay=0; Scanner sc=new Scanner(System.in); System.out.println("Enter Birth Year"); year=sc.nextInt(); if(year>2019) { System.out.println("Year Is Invalid Please Enter Proper Year"); } else { if(year%4==0) { leap=1; } System.out.println("Enter Birth Month"); month=sc.nextInt(); switch(month) { case 1: noOfDay=31; break; case 2: if(leap==1) noOfDay=29; else noOfDay=28; break; case 3: noOfDay=31; break; case 4: noOfDay=30; break; case 5: noOfDay=31; break; case 6: noOfDay=30; break; case 7: noOfDay=31; break; case 8: noOfDay=31; break; case 9: noOfDay=30; break; case 10: noOfDay=31; break; case 11: noOfDay=30; break; case 12: noOfDay=31; break; } if(month>12) { System.out.println("Month Is Invalid Please Enter Proper Month"); } else { System.out.println("Enter Birth Date"); dt=sc.nextInt(); if(dt>noOfDay) { System.out.println("Date Is Invalid Please Enter Proper Date"); } else { System.out.println("Your Birthday Is "+dt+"/"+month+"/"+year); } } } } }
- Output
Enter Birth Year 2000 Enter Birth Month 12 Enter Birth Date 31 Your Birthday Is 31/12/2000