L
Larry Coon
I understand the differnece between Date and Calendar, and why
I would prefer to use Calendar. However, I'm stuck with Date
in this instance.
I want to add a secific number of months to a Date. Looks like
I do it by converting to a Calendar, using Calendar.add(), and
converting back to Date. The following SSCCE isn't working for
me -- endDate is unchanged from beginDate. Can anyone point out
what I'm doing wrong (or if there's a better approach)? Thanks.
import java.text.*;
import java.util.*;
public class Test {
public static void main(String[] args) {
DateFormat format = new SimpleDateFormat("mm/dd/yyyy");
Date beginDate = format.parse("10/01/2005", new ParsePosition(0));
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTime(beginDate);
calendar.add(Calendar.MONTH, 3);
Date endDate = calendar.getTime();
System.out.println("Begin: " + format.format(beginDate));
System.out.println("End: " + format.format(endDate));
}
}
I would prefer to use Calendar. However, I'm stuck with Date
in this instance.
I want to add a secific number of months to a Date. Looks like
I do it by converting to a Calendar, using Calendar.add(), and
converting back to Date. The following SSCCE isn't working for
me -- endDate is unchanged from beginDate. Can anyone point out
what I'm doing wrong (or if there's a better approach)? Thanks.
import java.text.*;
import java.util.*;
public class Test {
public static void main(String[] args) {
DateFormat format = new SimpleDateFormat("mm/dd/yyyy");
Date beginDate = format.parse("10/01/2005", new ParsePosition(0));
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTime(beginDate);
calendar.add(Calendar.MONTH, 3);
Date endDate = calendar.getTime();
System.out.println("Begin: " + format.format(beginDate));
System.out.println("End: " + format.format(endDate));
}
}