Givena a date - get the next yr
Hello All,
I have a requirement such that given a date i have to get the next future date i.e., 1 yr from the given date..I'm using SimpleDateFormat:
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Date date = sdf.parse("givenDate");
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(date );
int year = cal.get(Calendar.YEAR + 1); I know that we can get the next year but I need a date 1 year in future keeping the leap year logic too...
Any help is greatly appreciated..
Thanks
[556 byte] By [
Maithria] at [2007-11-27 5:55:33]

How are you defining 1 year from the date ?
365 days away ?
366 days away if the leap year is included ?
the same date and you want to know the day of the week ?
52 weeks from that date ?
That is kind of important to the calculation. Give some input dates and some expected output dates might get you a better answer.
Add 1 to the Calendar's year field, then retrieve the Date from it. What you're doing here is wrong:
int year = cal.get(Calendar.YEAR + 1);
You're asking for some unspecified field, not year. I think what you were trying to do is this:
int year = cal.get(Calendar.YEAR) + 1;
but that won't give you the correct result.
Use this to add 1 to the year, then retreive the Date:
cal.add(Calendar.YEAR, 1);
Date date = cal.getTime();
Be sure to use the correct time zone or locale for your calendar too.
Edit: Wow, this is like JosAH grade slowness :)
Message was edited by:
hunter9000
Thank you all for your replies ...I'm doing something like this ...Please let me knoe if this is correct
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Date date = sdf.parse(dateStr);
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(date);
cal.add(Calendar.YEAR, 1);
String dateStr1 = sdf.format(cal.getTime());
Thanks,
Maithri