import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/**
* <p>Title : DateUtil.java</p>
* <p>Description : 時間操作工具類</p>
* <p>DevelopTools : IntelliJ IDEA 2018.2.3 x64</p>
* <p>DevelopSystem : Windows 10</p>
* <p>Company : org.wcy</p>
* @author : WangChenYang
* @date : 2019/8/31 17:01
* @version : 0.0.1
*/
public class DateUtil {
public enum DateMoveType {
YEAR(1,"年"),
MONTH(2,"月"),
DAY(5,"日"),
HOUR(11,"小時"),
MINUTE(12,"分鐘"),
SECOND(13,"秒"),
MILLISECOND(14,"毫秒");
private Integer value;
private String desc;
DateMoveType(Integer value,String desc){
this.value = value;
this.desc = desc;
}
public Integer getValue(){
return this.value;
}
}
public enum Format {
YYYY("yyyy","年"),
YYYY_MM("yyyy-MM","月"),
YYYY_MM_DD("yyyy-MM-dd","日"),
YYYY_MM_DD_HH("yyyy-MM-dd HH","時"),
YYYY_MM_DD_HH_MM("yyyy-MM-dd HH:mm","分"),
YYYY_MM_DD_HH_MM_SS("yyyy-MM-dd HH:mm:ss","秒");
private String value;
private String desc;
Format(String value,String desc){
this.value = value;
this.desc = desc;
}
public String getValue(){
return this.value;
}
}
/**
* 傳入需要推移的天數獲取日期
* @param date 需要推移的日期, setTime 推移的天數, type 需要推移的類型, format 日期格式的類型
* @return: 日期
* @Author: 王晨陽
* @Date: 2019/8/19-11:29
*/
public static String moveTime(Date date, int setTime, DateUtil.DateMoveType type, DateUtil.Format format) {
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
calendar.add(type.getValue(),setTime);//把日期往前減少一天,若想把日期向後推一天則將負數改爲正數
date=calendar.getTime();
SimpleDateFormat formatter1 = new SimpleDateFormat(format.getValue());
return formatter1.format(date);
}
public static void main(String[] args) {
System.out.println(moveTime(new Date(), 30, DateMoveType.MINUTE, Format.YYYY_MM_DD_HH_MM_SS));
}
}