Add a number of minutes to a date returning a new object in Java
Description
The following code shows how to add a number of minutes to a date returning a new object.
Example
// w w w. j a va 2 s . c o m
import java.util.Calendar;
import java.util.Date;
public class Main {
public static Date addMinutes(Date date, int amount) {
return add(date, Calendar.MINUTE, amount);
}
private static Date add(Date date, int calendarField, int amount) {
if (date == null) {
throw new IllegalArgumentException("The date must not be null");
}
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(calendarField, amount);
return c.getTime();
}
public static void main(String[] argv) {
Date date = new Date();
System.out.println(addMinutes(date, 100));
}
}
The code above generates the following result.