获取当前或者未来时间
public class TimeClass {
public static void main(String[] args) {
// 获取一个小时以后的时间
Calendar calendar = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
calendar.set(Calendar.HOUR_OF_DAY,
calendar.get(Calendar.HOUR_OF_DAY) + 1);
System.out.println("当前的时间:" + df.format(new Date()));
System.out.println("一个小时前的时间:" + df.format(calendar.getTime()));
// 获取一秒以后
calendar = Calendar.getInstance();
calendar.set(Calendar.SECOND,
calendar.get(Calendar.SECOND) + 1);
df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("一秒以后的时间:" + df.format(calendar.getTime()));
// 获取一天以后的时间
calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_YEAR,
calendar.get(Calendar.DAY_OF_YEAR) + 1);
df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("一天以后的时间:" + df.format(calendar.getTime()));
// 获取一个星期以后的时间
calendar = Calendar.getInstance();
calendar.set(Calendar.WEEK_OF_YEAR,
calendar.get(Calendar.WEEK_OF_YEAR) + 1);
df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("一个星期以后的时间:" + df.format(calendar.getTime()));
// 获取一个月以后的时间
calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, 1);
df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("一个月以后的时间:" + df.format(calendar.getTime()));
}
}
转载