public setProjectedGraduationDate(GregorianCalendar projectedGraduationDate){
projectedGraduationDate.setTime(this.enrollmentDate);
projectedGraduationDate.add(Calendar.YEAR, 4);
}
Trying to set a GregorianCalendar variable = to another GregroianCalendar variable + 4 years
-
20-06-2023 - |
题
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.
解决方案 2
其他提示
just execute getTime()
to get Date
instance out of GregorianCalendar
The add
method of the GregorianCalendar
class is void, so you can't assign a call to it to a variable.
If your intention is to modify the GregorianCalendar
object that you're passing in to your method, then you'd want to write
projectedGraduationDate.add(calendar.YEAR,4);
but this would require setting projectedGraduationDate
to the right value first.