hi guys how r u doing ..
i am trying something in java ..
i want to know if there is a way to the java looks into the current year. i want to be to return the age of someone after filling in
his birth year .. and then i want to substract his birth year from the current year in order to get his age.

i just need to know what am i supposed to import
and what to do in order for java to see the current year.

thanks in advance

Recommended Answers

All 12 Replies

Check the API on GregorianCalendar (or just Calendar if you wish).

Also keep in mind the forum rules regarding "chat speak" - if you wish to ask for help, take the time to type all of the letters in your words and use capital letters where appropriate.

Thanks for the help and I will take that into consideration.

I am getting the facing the following problem
it always subtracts the birth year from the current year.
I have been trying to let it check whether the birth day has passed this year or not. And if it didnt pass yet, I want to substract 1 from the outcome.
This is the code i am using

LeefTijd is by the way a field (it is an int)
and as for the birthday i used private int and not private Calender

public int getLeefTijd()
    {
        // Maak een object aan met de geboortedatum
        Calendar geboorteDatum = new GregorianCalendar();
        // Maak een object aan met de datum van vandaag
        Calendar vandaag = Calendar.getInstance();
        // leeftijd op basis van jaren
        int leeftijd = vandaag.get(Calendar.YEAR) - GeboorteJaar;
        //geboorteDatum.add(Calendar.YEAR, leeftijd);
        // als de verjaardag dit jaar niet geweest is, trek de 1 af van de leeftijd
        if (vandaag.before(geboorteDatum))
        {
            leeftijd--;
            LeefTijd = leeftijd;
            return LeefTijd;
        }
        else
        {
            LeefTijd = leeftijd;
            return LeefTijd;
        }
    }

I don't know if it is just me but I didn't understood anything from your program Could you please change the names of the variables to English?

Thanks.

Sorry :icon_redface:

public int getLeefTijd()
    {
        // make an object with the birth date
        Calendar birthDate = new GregorianCalendar();
        // make an object with today's date
        Calendar today = Calendar.getInstance();
        // age according to year
        int age = today.get(Calendar.YEAR) - BirthYear; // birth year is a field that was filled in by the user
        // check if this year's birthday passed. if not age -1
        if (today.before(birthDate))
        {
            age--;
            Age = age;          // Age is also a field
            return Age;
        }
        else
        {
            Age = age;
            return Age;
        }
    }

So where is the problem ??

the problem is the following:

lets say i fill the following birthday in: 3/8/1984
the result that i get for the age is 25 and not 24.
it is not substracting

if (today.before(birthDate))


You're passing a GregorianCalendar Object to the before method. Are you sure this is correct? I wouldn't really know, but I don't see how a calendar and a day are the same thing.... you should be passing a day to the before method, probably.

edit: Yeah, it seems like you have it correct. Hm

It isn't working because you never initialized the 'birthDate' variable to the person's birthday.

public int getLeefTijd()
    {
        // make an object with the birth date
        Calendar birthDate = new GregorianCalendar();
        
        if (today.before(birthDate))
        {
            age--;
            Age = age;          // Age is also a field
            return Age;
        }
      
    }

So birthDate just has whatever the default values are. . not the person's birthday like you want it to have. To fix it, you could use this constructor, "GregorianCalendar(int year, int month, int date)" instead of the default no parameter constructor you're currently using.

Calendar birthDate = new GregorianCalendar(); Aren't you suppose to set values to that object the input from the user?

It isn't working because you never initialized the 'birthDate' variable to the person's birthday.

We posted the same time :)

lol .. thanks a lot for help guys ..

I am sorry .. while I was waiting for the answers I solved it and I was so into programming that I forgot to post that I solved it.

Sorry again.

Anyways I did the following:

Calendar birtheDate = new GregorianCalendar(BirthYear, BirthMonth, BirthDay);

These three Variable (BirthYear and the rest are all fields and not local variables that are filled in at the beginning of the application.
This way the user does not need to refill them in in order to get the age.

I tried that a couple of times it all worked fine. Except with my dad's birthday lol .. It gives me a wrong age. But all other attempts were succesful.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.