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]
# 1
cal.add(Calendar.YEAR, 1);
jverda at 2007-7-12 16:24:32 > top of Java-index,Java Essentials,Java Programming...
# 2

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.

Aknibbsa at 2007-7-12 16:24:32 > top of Java-index,Java Essentials,Java Programming...
# 3
Note: cal.get(Calenday.YEAR + 1); does NOT get the current year plus 1. It gets the current value of whatever field happens to be denoted by a constant one greater than the contstan YEAR.YEAR is just a constant. It's NOT the current YEAR. Its value has no meaning to you.
jverda at 2007-7-12 16:24:32 > top of Java-index,Java Essentials,Java Programming...
# 4
Got it ....Thanks for all your replies...
Maithria at 2007-7-12 16:24:32 > top of Java-index,Java Essentials,Java Programming...
# 5

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

hunter9000a at 2007-7-12 16:24:32 > top of Java-index,Java Essentials,Java Programming...
# 6
Another question is how precise do you have to be? Are days enough, or is the quarter day on the end of each calander that the sun has to catch up (the reason for leap year) important? Do you need to know down to the millisecond?PS.
puckstopper31a at 2007-7-12 16:24:32 > top of Java-index,Java Essentials,Java Programming...
# 7

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

Maithria at 2007-7-12 16:24:32 > top of Java-index,Java Essentials,Java Programming...