Search code examples
javadatedatetimecalendargregorian-calendar

Trying to set a GregorianCalendar variable = to another GregroianCalendar variable + 4 years


I am having through getting setting the value of a GregorianCalendar to that of another one + 4 years. I see there s the add method in the class but this accepts an int and I am trying to pass a GregorianCalendar type.

public GregorianCalendar getEnrollmentDate(GregorianCalendar enrollmentDate){
    return enrollmentDate;
}
public void setProjectedGraduationDate(GregorianCalendar projectedGraduationDate){
    Calendar cal = new GregorianCalendar();
    cal.setTime(this.enrollmentDate);
    projectedGraduationDate = cal.add(Calendar.YEAR, 4);
}

The value that I am trying to add years to is "enrollmentDate"

Is this possible, the "setTime" method accepts a "Date" time, not a GregorianCalendar though.


Solution

  • public setProjectedGraduationDate(GregorianCalendar projectedGraduationDate){
        projectedGraduationDate.setTime(this.enrollmentDate);
        projectedGraduationDate.add(Calendar.YEAR, 4);
    }