Java age in days

By | 17/11/2014

Program ask for your birthday and current date and calculates your age in days.

AgeInDays.java

import java.util.Scanner;

public class AgeInDays {
 Scanner reader=new Scanner(System.in);
 int yearOfBirth, monthOfBirth, dayOfBirth, currentYear, currentMonth, currentDay, input;//Scope
 private void run() {		
  yearOfBirth=getYear("year of birth");
  monthOfBirth=getMonth("month of birth");
  dayOfBirth=getDay("day of birth");
  currentYear=getYear("current year");
  currentMonth=getMonth("current month");
  currentDay=getDay("current day");
  reader.close();
		
  int ageInDays=countAge(currentYear, yearOfBirth, currentMonth, monthOfBirth, currentDay, dayOfBirth);
  printAge(ageInDays);		
 }
 private int getDay(String day) {
  input=0;
  do {
   System.out.println("Enter "+day);
   input=reader.nextInt();
  }while(input>30 || input<1);
  return input;
 }
 private int getMonth(String month) {
  input=0;
  do {
   System.out.println("Enter " +month);
   input=reader.nextInt();			
  }while(input>12 || input<1);
  return input;
 }
 private int getYear(String year) {
  input=0;
  do {
   System.out.println("Enter " +year);
   input=reader.nextInt();
  }while(input<=0);
  return input;
 }
 private int countAge(int currentYear, int yearOfBirth,int currentMonth, int monthOfBirth,int currentDay, int dayOfBirth) {
  int year=(currentYear-yearOfBirth)*360;
  int month=(currentMonth*30)-(monthOfBirth*30);
  int day=currentDay-dayOfBirth;
  input = year+month+day;
  return input;
 }
 private void printAge(int age) {
  System.out.println("You are "+age+" days old");
 }
	
 public static void main(String[] args) {
  AgeInDays program = new AgeInDays();
  program.run();
 }
}

Leave a Reply

Your email address will not be published. Required fields are marked *