1

Possible Duplicate:
How to add days to a date in Java

I need to get the next date, on click of next link, and get the previous date on click of previous link. How do I increment or decrement dates? I am not getting how to work with date to increment/decrement it.

Community
  • 1
  • 1
user1195292
  • 233
  • 1
  • 3
  • 13

2 Answers2

2
Calendar cal = Calendar.getInstance();
DateFormat formatter = new SimpleDateFormat("dd-MMM-yy");
Date date = formatter.parse(your_String_date);
cal.setTime(date);

Now do the below as shown:

for increment

cal.add( Calendar.DATE, 1 );

for decrement

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

If you want to increment/decrement more days then just replace 1 by your number of days.

Hope it will help you.

Here is the Calendar class of java.

Android Killer
  • 18,174
  • 13
  • 67
  • 90
1

You might want to use JodaTime instead of the standard date api, since it's easier to use and a JodaTime-like api will be added to Java 8 anyways.

With the standard api have a look at the Apache Commons Lang library and especially its DateUtils class, which has a addDays(date, numberOfDays) method.

Thomas
  • 87,414
  • 12
  • 119
  • 157