直接上代码:
public class DateUtil {
/**
* 获取延迟后的时间
*
* @param date
* 需要处理的时间
*
* @param timeTypeEnum
* 时间类型
*
* @param delta
* 需要顺延的时间变量
*
* @return Date
* */
public static Date getContinueDate(Date date, TimeTypeEnum timeTypeEnum, int delta) {
if (null == date) {
return null;
}
// 向后顺延相应的天数
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
if (timeTypeEnum == TimeTypeEnum.DAY) {
calendar.add(calendar.DATE, delta); // 把日期往后增加N天, 正数往后推,负数往前移动
} else if (timeTypeEnum == TimeTypeEnum.HOUR) {
calendar.add(calendar.HOUR, delta);// 把日期往后增加N小时, 正数往后推,负数往前移动
} else if (timeTypeEnum == TimeTypeEnum.MINUTE) {
calendar.add(calendar.MINUTE, delta);// 把日期往后增加N分钟, 正数往后推,负数往前移动
} else if (timeTypeEnum == TimeTypeEnum.MONTH) {
calendar.add(calendar.MONTH, delta); // 把日期往后增加N个月, 正数往后推,负数往前移动
} else if (timeTypeEnum == TimeTypeEnum.YEAR) {
calendar.add(calendar.YEAR, delta); // 把日期往后增加N年, 正数往后推,负数往前移动
}
return calendar.getTime();
}
}
/**
* 时间类型枚举类
*
* */
public enum TimeTypeEnum {
YEAR(1, "年"), MONTH(2, "月"), DAY(3, "日"), HOUR(4, "小时"), MINUTE(5, "分"), SECOND(6, "秒");
private int timeType;
private String desc;
private TimeTypeEnum(int timeType, String desc) {
this.timeType = timeType;
this.desc = desc;
}
public int getTimeType() {
return timeType;
}
public void setTimeType(int timeType) {
this.timeType = timeType;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}