PDA

View Full Version : get exact next year date


charon
08-08-2007, 11:48 AM
Hi,

I'm having problem in getting the exact next year date. I.e:

Existing Expired Date - 02/12/2007

Next Expired Date - 01/12/2007

I'm trying to use:
Date date = sdf.parse("02/12/2009");
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(date);
cal.add(Calendar.YEAR, 1);
String dateStr1 = sdf.format(cal.getTime());
out.print("Next Year Date " + dateStr1);

But the result is 02/12/2007, which is wrong.

Pls advise.......

ess
08-08-2007, 01:29 PM
Hi charon,

Here is a sample code that demonstrates how you could add another year to calendar which was originally created from a date instance.


import java.util.*;
import java.text.*;
public class JavaDate {
public static void main(String[] args) {
DateFormat dateFormat = new SimpleDateFormat("dd/mm/yyyy");
// Note: using UK Locale, might be different for you
GregorianCalendar calendar = new GregorianCalendar(Locale.UK);
Date date = null;
try {
date = dateFormat.parse("02/12/2007");
} //-- ends try block
catch (ParseException e) {
e.printStackTrace(System.err);
} //-- ends catch block
// set the date
calendar.setTime(date);
// print original date
System.out.println("Original Date: " + dateFormat.format(calendar.getTime()));
// add one year to the calendar
calendar.set(GregorianCalendar.YEAR, calendar.get(Calendar.YEAR)+1);
// print date
System.out.println("One Year Later: " + dateFormat.format(calendar.getTime()));

// set date to original value...just as an example to
// go two years forward instead of one year forward
calendar.setTime(date);
// go two years up
calendar.set(GregorianCalendar.YEAR, calendar.get(Calendar.YEAR)+2);
// print it
System.out.println("two Years Later: " + dateFormat.format(calendar.getTime()));
} //-- ends main
} //-- ends class


and here is the output


Original Date: 02/12/2007
One Year Later: 02/12/2008
two Years Later: 02/12/2009


hope that helps

cheers,
Ess

charon
08-08-2007, 01:58 PM
Hi,

I'm having problem in getting the exact next year date. I.e:

Existing Expired Date - 02/12/2007

Next Expired Date - 01/12/2008 - this is the result that I want

I'm trying to use:
Date date = sdf.parse("02/12/2009");
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(date);
cal.add(Calendar.YEAR, 1);
String dateStr1 = sdf.format(cal.getTime());
out.print("Next Year Date " + dateStr1);

But the result is 02/12/2008, which is wrong.

Pls advise.......

Hi, my example is most or less same as yours, but it won't generate the result that I want, what I want is ~ 01/12/2008

pls advise....

ess
08-08-2007, 02:54 PM
Read the code I provided above...it clearly outputs the expected results.

charon
08-08-2007, 06:01 PM
Thanks, manage to get the result by just add:

cal.add(Calendar.DAY_OF_YEAR, -1);

:)

jkim
08-20-2007, 03:47 AM
I think your solution is wrong.

2008 is a leap year, which is why maybe you want 01/12/2008. If it was not a leap year, would you want 01/12? or 02/12?

I'd suggest adding 365 days rather than adding a year and then - 1 day, because that is very inconsistent.