CALENDAR/DATE trouble... I think you can help me on this one.

Hi!

My problem is given the date today… I want to subtract 2 days from it…
And also for example i get

dateToday.getDay()
…getMonth()
…getYear()
Result when i print them:
Month:5
Day: 3
Year: 105

this is the actual date:
Wed Jun 29 12:14:03 GMT+08:00 2005

i remember my prof said that just add example in the year its 105, to make it 2005 just add 1900. But the problem rises in the DAY… the actual DAY is 29 and i got 3 so i added 26 to 3 to make it 29 but when i changed the month day+26 is wrong…

Pls help me on this

Thanks so much!

Hi
Most methods of java.util.Date are depricated and a better alternative would be to use java.util.GregorianCalendar instead and use java.text.SimpleDateFormat to format your output as necessary.

// Gregof

getDay() actually returns the day of the week, not the day of the month. Use getDate() instead.

Calendar c = new GregorianCalendar();
c.add(Calendar.DATE, -2);
Date twoDaysAgo = c.getTime();

Also note that in Java months start at 0, so Jan = 0, Feb = 1…Dec = 11, so Jun =5 is correct.

Andy.