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