Question

I am using a JXDatepicker to get the birth date of a person, and what i have to do is set another JXDatepicker with the retirement date. So the idea here is to get the year of the birth date and to increment it with one year in a loop until the "person" reaches 60 years old and keep the same day and month of birth.. Or i think that's what i have to do. but i can't seem to find how to use the Calendar classes nor the DateFormat classes to do this calculation, i tried different things but nothing works, the closest i got is the date 01/01/1970, which can't be the person's retirement date since the birth date i'm testing is always in 2014 so it should be 2074.

So how can i do the calculation? I'd be grateful if someone could give me an example on how to do it.

(It might seem that i didn't do enough researches, but i did, and i don't find why i would mention what i found since it didn't help me)

Était-ce utile?

La solution

You shouldn't write any loop to add 60 years to your date. You can get a selected date from your JXDatePicker and do it like that:

final Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.YEAR, 60);
final Date retirementDate = cal.getTime();

Autres conseils

Why on earth would you have to add 1 in a loop 60 times? Why don't you just add 60?

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class Main {

    public static void main(String... args){

        SimpleDateFormat df = new SimpleDateFormat("MM/dd/YYYY");

        Calendar c = Calendar.getInstance();
        System.out.println(df.format(c.getTime())); //prints 05/14/2014

        c.add(Calendar.YEAR, 60);
        System.out.println(df.format(c.getTime())); //prints 05/14/2074
    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top