Create Age Calculator Program in Java

Hello there, I'm Himanshu. Today I am going to share my new program Age Calculator which is written in Java Language.you can download this code and you can also look upon other amazing programs here. Any comments and suggestions are most welcome.

About Program



Age Calculator is a simple program that calculates your age from your birth date to current date.
You simply have to enter your date of birth and this program will gives your age at current date.

Let's Begin
I have used many comments in my program so that you can easily understand that and learn from that.
Now I'm going to explain my code.
For This program i'm going to use these modules
   
  1. //importing modules
  2.  import java.util.Scanner; //Scanner class for taking input from user
  3. import java.time.LocalDateTime; //Library file for time and date from system

Now, I have created two classes  main class and input class.

Main class

This class is used to take current date from system and take the DOB from user. It also create the object of input class and call it's methods to find the results.

  1. class main
  2. {
  3.    public static void main(String args[])
  4.     {   input user=new input();  //creating object
  5.          LocalDateTime obj=LocalDateTime.now();
  6.          Scanner sc=new Scanner(System.in);                
  7.         
  8.         int date,month,year;         //variables that store data from system 
  9.         int udate,umonth,uyear;   //variables that store data from user
  10.         int adate,amonth,ayear;    //variables that store age
  11.         int day=0;      //use to count no. of days in month 
  12.         int i=0;
  13.         
  14.         //date,month,year form system
  15.         date=obj.getDayOfMonth();        
  16.         month=obj.getMonthValue();
  17.         year=obj.getYear();
  18.        
  19.        //taking input from user 
  20.           i=user.getdob();     //taking input by calling getdob() method
  21.   
  22.   //checking form wrong input
  23.   while(i!=1)
  24.   {   System.out.println("Entered data is not valid  please enter again ");
  25.   i=user.getdob();
  26.   }
  27.   
  28. udate=user.givedate();
  29. umonth=user.givemonth();
  30. uyear=user.giveyear();
  31. day=user.giveday();
  32.   
  33.          //calculating age         
  34.           ayear=year-uyear-1;
  35.           amonth=12-(umonth-month)-1;
  36.           adate=day-udate+date;
  37.    
  38.           if(adate>=day)
  39.             {
  40.                 adate=adate-day;            
  41.                 amonth=amonth+1;
  42.             }  
  43.           if(amonth>=12)
  44.             {
  45.                 amonth=amonth-12;
  46.                 ayear=ayear+1;
  47.              }
  48.             
  49.           
  50.         System.out.println("age: "+ayear+" year "+amonth+" month "+adate+" day");
  51.         
  52.         if(adate==0 && amonth==0)
  53.             System.out.println("Happy Birthday Dear");
  54.         
  55.     }
  56. }

Now, input class

This class takes input form user and send data to main class. It contains methods getdob() , givedate() ,givemonth(), giveyear() and giveday().

  1. class input
  2. { int udate,umonth,day,i,uyear;
  3. Scanner sc=new Scanner(System.in);                
  4.         String str=new String();   
  5. //this class is used to take dob from user and validate dob
  6. public int getdob()
  7. {
  8. System.out.println("Enter date of birth in dd-mm-yyyy format");
  9. str=sc.nextLine(); 
  10. if(str.length()==10 && str.charAt(2)=='-' && str.charAt(5)=='-')
  11.   {
  12.   udate=Integer.valueOf(str.substring(0,2));
  13.   umonth=Integer.valueOf(str.substring(3,5));
  14.   uyear=Integer.valueOf(str.substring(6,10)); 
  15.   
  16.   switch(umonth)
  17. {
  18. case 1:case 3: case 5: case 7: case 8: case 10:case 12:day=31;break;
  19. case 4:case 6: case 9:case 11:day=30;break;
  20. case 2:
  21. if(uyear%4==0)
  22. day=29;
  23. else
  24. day=28; 
  25. }
  26.   if(udate>day || umonth>12){i=0;}
  27.   else i=1;
  28.   }
  29. else 
  30.    i=0;
  31. return i;
  32. }
  33.   //below methods are use to transfer data into main function for processing
  34. public int givedate()
  35. {
  36. return udate;
  37. }
  38. public int givemonth()
  39. {
  40. return umonth;
  41. }
  42. public int giveyear()
  43. {
  44. return uyear;
  45. }
  46. public int giveday()
  47. {
  48. return day;
  49. }
  50. }



You can Download the whole code from here.
If you have any query than feel free to ask in comment section.