0% found this document useful (0 votes)
70 views2 pages

Calculate Age in Java

This Java program calculates a person's age in years, months, and days by asking the user to input their date of birth and then comparing it to a set date. It uses an array to store the number of days in each month and calculates the age by subtracting the birth date fields from the current year, month, and date. The output prints the calculated age fields to the user.

Uploaded by

Rahul Pal
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views2 pages

Calculate Age in Java

This Java program calculates a person's age in years, months, and days by asking the user to input their date of birth and then comparing it to a set date. It uses an array to store the number of days in each month and calculates the age by subtracting the birth date fields from the current year, month, and date. The output prints the calculated age fields to the user.

Uploaded by

Rahul Pal
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

1 of 2

http://java90.blogspot.in/2011/07/calculate-age-in-java.html

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package age;
import java.util.Scanner;
/**
*
* @author ACHCHUTHAN
*/
public class AgeCalculate {
int month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
String id;
int d, m, y;
public AgeCalculate() {
Scanner input = new Scanner(System.in);
System.out.println("Enter your day=");
d = input.nextInt();
System.out.println("Enter your month=");
m = input.nextInt();
System.out.println("Enter your year=");
y = input.nextInt();
}
public void CalculateAge() {
int date = 0, mon = 0, year = 0;
if (1 < d && 1 < m) {
date = ((month[m - 1] + 01) - d);
mon = (12 - m);
year = (2012 - y);
} else if (1 < d && 1 >= m) {
date = ((month[m - 1] + 01) - d);
mon = (12 - m);
year = (2012 - y);
} else if (d == 1 && 1 < m) {
date = 1 - d;
mon = (13 - m);
year = (2010 - y);
} else if (d == 1 && m == 1) {
date = 1 - d;
mon = (13 - m);
year = (2012 - y);
}
System.out.println(year + " years " + mon + " months " + date + "
days");
}
public static void main(String args[]) {

3/2/2012 8:29 PM

2 of 2

http://java90.blogspot.in/2011/07/calculate-age-in-java.html

53
54
55
56
57 }

AgeCalculate a = new AgeCalculate();


System.out.println("Your age calculated on 2012/01/01");
a.CalculateAge();
}

3/2/2012 8:29 PM

You might also like