Advertisement
Guest User

Age calculator

a guest
Jun 16th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.61 KB | None | 0 0
  1. import javax.swing.JOptionPane;
  2.  
  3. public class AgeCalculation
  4. {
  5.     public static void main(String[] args)
  6.     {
  7.        
  8.         String takeBirthDay = JOptionPane.showInputDialog("Enter Birthday(dd): ");
  9.         String takeBirthMonth = JOptionPane.showInputDialog("Enter Birth Month(mm): ");
  10.         String takeBirthYear = JOptionPane.showInputDialog("Enter Birth Year(yyyy): ");
  11.         String takePresentDay = JOptionPane.showInputDialog("Enter Present day(dd): ");
  12.         String takePresentMonth = JOptionPane.showInputDialog("Enter Present Month(mm): ");
  13.         String takePresentYear = JOptionPane.showInputDialog("Enter Present Year(yyyy): ");
  14.        
  15.         int BirthDay = Integer.parseInt(takeBirthDay);
  16.         int BirthMonth = Integer.parseInt(takeBirthMonth);
  17.         int BirthYear = Integer.parseInt(takeBirthYear);
  18.         int PresentDay = Integer.parseInt(takePresentDay);
  19.         int PresentMonth = Integer.parseInt(takePresentMonth);
  20.         int PresentYear = Integer.parseInt(takePresentYear);
  21.        
  22.         int month[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  23.      
  24.         if (BirthDay > PresentDay)
  25.         {
  26.             PresentDay = PresentDay + month[BirthMonth - 1];
  27.             PresentMonth = PresentMonth - 1;
  28.         }
  29.      
  30.         if (BirthMonth > PresentMonth)
  31.         {
  32.             PresentYear = PresentYear - 1;
  33.             PresentMonth = PresentMonth + 12;
  34.         }
  35.      
  36.         int calculated_date = PresentDay - BirthDay;
  37.         int calculated_month = PresentMonth - BirthMonth;
  38.         int calculated_year = PresentYear - BirthYear;
  39.      
  40.        
  41.         JOptionPane.showMessageDialog(null,"Your age is "+ calculated_date +" days, "+ calculated_month +" months and "+ calculated_year +" years.", "Result", JOptionPane.PLAIN_MESSAGE);
  42.     }
  43.    
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement