Roll java.util.Date back and forth
import java.util.GregorianCalendar;
public class Main {
/**
* Roll the java.util.Date forward or backward.
* @param startDate - The start date
* @period Calendar.YEAR etc
* @param amount - Negative to roll backwards.
*/
public static java.util.Date rollDateTime( java.util.Date startDate, int period, int amount )
{
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(startDate);
gc.add(period, amount);
return new java.util.Date(gc.getTime().getTime());
}
}
Home
Java Book
Runnable examples
Java Book
Runnable examples
Date Set:
- Set Date to Noon
- Set Date to the first millisecond of the day, just after midnight.
- Set Date to the first millisecond of the month, just after midnight.
- Set Date to the last millisecond of the day, just before midnight.
- Set Date to the last millisecond of the minute.
- Set Date to the last millisecond of the month, just before midnight.
- Set date to clear the time values
- Set date to the end of the day
- Set Date to the start of the day
- Set date to the end of a day
- Set date to the start of an hour
- Set date to then end of an hour
- Set date to the start of a minute
- Set date to the end of a minute
- Set Calendar to Mid night
- Set year, month and day to a date
- Roll java.util.Date back and forth