This code will accept from the user the current date, month, and year; and their birth date, birth month, birth year. It outputs the age of the person in years, months and days. The code as I have written it does its job. I have cross-checked with a website on the internet. I want to know:
How do I implement the Date class in Java to get the current Date, Month and Year? Will I be better off with Swing or AWT kits, if I want to make a GUI?
import java.util.*;
class AgeCalc
{
static int[] MonthDays = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
public static void Input(int cd, int cm, int cy)
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter Birth Date");
int bd = scan.nextShort();
System.out.println("Enter Birth Month");
int bm = scan.nextShort();
System.out.println("Enter Birth Year");
int by = scan.nextShort();
int fnd = (DaysofMidYears(by, cy)) + Birth_CurrYearDays(bd, bm, by, cd, cm, cy);
int y = fnd / 365;
int m = (fnd - (y * 365)) / 30;
int d = (fnd - (y * 365) - (m * 30));
System.out.println("No. of Days: "+d);
System.out.println("No. of Months: "+m);
System.out.println("No. of Years: "+y);
}
public static boolean LeapYearTest(int y)
{
boolean lys =
( ((y % 400) == 0) ? true
: ((y % 10) == 0) ? false
: ((y % 4) == 0) ? true
: false );
return lys;
}
public static int DaysofMidYears(int by, int cy)
{
int dmy = 0;
for
(int i = (by + 1); i <= (cy - 1); i++)
{
dmy += 365;
if
((LeapYearTest(i)) == true)
{
dmy += 1;
}
}
return dmy;
}
public static int InitialDaysofYear(int d, int m, int y)
{
int id = d;
for
(int i = 1; i <= (m - 1); i++)
{
id += (MonthDays[i - 1]);
}
if
(((LeapYearTest(y)) == true) && (m > 2))
{
id += 1;
}
return id;
}
public static int Birth_CurrYearDays(int bd, int bm, int by, int cd, int cm, int cy)
{
int bcyd = 0;
bcyd += (InitialDaysofYear(cd, cm, cy));
bcyd += (365 - (InitialDaysofYear(bd, bm, by)));
if
((LeapYearTest(by)) == true)
{
bcyd += 1;
}
return bcyd;
}
}