时间
java设置日期的方法:
注意,如果需要获取第n天前的日期,然后在继续获取其他时间的日期,需要将日历重置成今天的日期。
//获取输出当天日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); //设置 时间格式
Date today = new Date();
System.out.println("当天" + sdf.format(today));
//获取n天前的日期
Calendar calendar = Calendar.getInstance();
calendar.setTime(today);//获取当前时间日历
calendar.add(Calendar.DAY_OF_MONTH, -50);//-n代表n天前。+n代表n天后
Date yesterday = calendar.getTime();
System.out.println("50天前:" + sdf.format(yesterday));
//获取n天之后的日期:
calendar.setTime(today);
calendar.add(Calendar.DAY_OF_MONTH, +11);
Date future = calendar.getTime();
System.out.println("第11天后:" + sdf.format(future));