import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* @author shafei
* @version 1.0
* @date 10:40 2019/9/27
* @fun
*/
public class GetDate {
public static void main(String[] args) {
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
.format(new Date()));
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
.format(dateRoll(new Date(), Calendar.HOUR, -1)));
}
public static Date dateRoll(Date date, int i, int d) {
// 获取Calendar对象并以传进来的时间为准
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
// 将现在的时间滚动固定时长,转换为Date类型赋值
calendar.add(i, d);
// 转换为Date类型再赋值
date = calendar.getTime();
return date;
}
}