The problem I have is the following. I have a Date, lets say today and I want to get add, for example, 120 days to get another Date object, which of course should point 120 days into the future.
I know how to parse Strings into Date objects and vice versa. I have done it before a while ago and it worked going about it like this. I get the date. I get a GregorianCalendar and use the setTime(Date d) method to the date and use then the Gregorians add(int, int) method to increment the days. I could also use roll(int, true).
It doesn`t work so. I have tried pretty much everything and the problem is I either get bogus, or the month and years dont get incremented when they should. I used Suns JDK 1.7B Y2K compliant and also 1.1.8. I use NT 4.0 with SP 3. Do you have any idea ? Or some code snippets which work for you. Please also tell me the JDK version and operating system.
Here comes my source
public class MainDateExamples
SimpleDateFormat sdf = new SimpleDateFormat("dd.mm.yyyy");
String d = new String("1.10.1999");
Date before = sdf.parse(d, new ParsePosition(0));
GregorianCalendar g = new GregorianCalendar();
// Doesn work either :( g.add(GregorianCalendar.DAY_OF_YEAR, 120);
Date after = g.getTime();
System.out.println("Before = " + d);
System.out.println("After = " + sdf.format(after));
String kkkk = null;
public final static void main(String args)
MainDateExamples md = new MainDateExamples();
The output is :
Before = 1.10.1999
After = 01.10.1999
and the loosser is me since I have no clue why.
What do you think ?