0

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;
    }
}
Riley Jones
  • 43
  • 1
  • 8
  • 1
    _How do I implement the Date class in Java to get the current Date, Month and Year?_ Do you need to write your own implementation of class `java.util.Date`? – Abra Dec 07 '19 at 05:52
  • 1
    _How do I make it a GUI?_ You need to write a GUI in java? If yes, then you have either [Swing](https://docs.oracle.com/javase/tutorial/uiswing/index.html) or [JavaFX](https://docs.oracle.com/javase/8/javafx/get-started-tutorial/jfx-overview.htm). – Abra Dec 07 '19 at 05:54
  • Welcome to Stack Overflow. How to make a GUI is way too broad for a Stack Overflow question. Fortunately there are already numerous tutorials on that subject out there. Use your search engine. – Ole V.V. Dec 07 '19 at 11:32
  • This looks like a typo: `(y % 10) == 0`. Should it be `y % 100`? And such a typo illustrates quite well why it’s better to use a proven standard library than to write your own logic for the task. – Ole V.V. Dec 07 '19 at 11:33
  • Does this answer your question? [How do I calculate someone's age in Java?](https://stackoverflow.com/questions/1116123/how-do-i-calculate-someones-age-in-java) If not, search for other existing questions that do. There most likely are more than one. – Ole V.V. Dec 07 '19 at 11:35

2 Answers2

3

We no longer use the java.util.Date or java.sql.Date classes. They were supplanted years ago by the modern java.time classes defined in JSR 310.

For a date-only value without time-of-day and without time zone, use LocalDate.

LocalDate birthDate = LocalDate.of(y, m, d);

For elapsed time, or age, use Period class.

LocalDate today = LocalDate.now();
Period age = Period.between(birthDate, today);
long years = age.getYears();
Clashsoft
  • 11,553
  • 5
  • 40
  • 79
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
1

Time calculations are tricky beasts, and it's best to avoid implementing custom Date/Time classes (except maybe for exercise or learning purposes). As for your question, use Java 8's java.time.* classes:

ZoneId timeZoneId = ZoneId.of("Europe/Warsaw"); // assuming you're in Poland - put the your time zone name here ;-)
LocalDate today = LocalDate.now(timeZoneId);
LocalDate birthday = LocalDate.of(year, month, day);
Period lifePeriod = Period.between(birthday, today);
long age = lifePeriod.getYears();

As for the GUI part of your question - it's "a bit" too broad :-) As a general advice - learn about JavaFX, and how to build UIs in it, if you want to use Java as your toolkit for GUI apps.

EDIT: Side note on codestyle and conventions - Java has a well established method and variable naming conventions - it's always a good practice to learn and use them. It makes your code more readable for those familliar with the conventions.

ttarczynski
  • 949
  • 1
  • 10
  • 19