public static void main(String[] args) {
System.out.println("hour==" + addTime(new Date(), "hour", 4));
System.out.println("minute==" + addTime(new Date(), "minute", 4));
System.out.println("day==" + addTime(new Date(), "day", 4));
System.out.println("second==" + addTime(new Date(), "second", 4));
}
public static Date addTime(Date date, String type, int time) {
Date addDate = date;
Calendar cal = Calendar.getInstance();
cal.setTime(date);
if (StringUtils.equals(type, "hour")) {
cal.add(Calendar.HOUR, time);
addDate = cal.getTime();
return addDate;
}
if (StringUtils.equals(type, "minute")) {
cal.add(Calendar.MINUTE, time);
addDate = cal.getTime();
return addDate;
}
if (StringUtils.equals(type, "second")) {
cal.add(Calendar.SECOND, time);
addDate = cal.getTime();
return addDate;
}
if (StringUtils.equals(type, "day")) {
cal.add(Calendar.DAY_OF_MONTH, time);
addDate = cal.getTime();
return addDate;
}
return addDate;
}
05-24