Roll the java.sql.Date forward or backward in Java
Description
The following code shows how to roll the java.sql.Date forward or backward.
Example
/*from www.j a v a 2 s .c o m*/
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class Main {
/**
* 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(rollDate(new Date(), Calendar.YEAR, 10));
}
}
The code above generates the following result.