Roll the days forward or backward in Java
Description
The following code shows how to roll the days forward or backward.
Example
/*ww w .jav a2 s. co m*/
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class Main {
/**
* Roll the days forward or backward.
*
* @param startDate
* - The start date
* @param days
* - Negative to rollbackwards.
*/
public static java.sql.Date rollDays(java.util.Date startDate, int days) {
return rollDate(startDate, Calendar.DATE, days);
}
/**
* Roll the java.sql.Date forward or backward.
*
* @param startDate
* - The start date
* @period Calendar.YEAR etc
* @param amount
* - Negative to rollbackwards.
*/
public static java.sql.Date rollDate(java.util.Date startDate, int period,
int amount) {
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(startDate);
gc.add(period, amount);
return new java.sql.Date(gc.getTime().getTime());
}
public static void main(String[] argv) {
System.out.println(rollDays(new Date(), 10));
}
}
The code above generates the following result.