//获取指定时间前N天后N天
@Test
// public void getLastDay(String time){
public void getLastDay(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
Date date=null;
try {
date = sdf.parse("2022-8-24");
// date = sdf.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
calendar.setTime(date);
int day=calendar.get(Calendar.DATE);
// 此处修改为+1则是获取后一天
// calendar.set(Calendar.DATE,day-3);
calendar.set(Calendar.DATE,day+3);
String lastDay = sdf.format(calendar.getTime());
System.out.println("lastDay = " + lastDay);
// return lastDay;
}
java获取指定时间前N天和后N天
最新推荐文章于 2023-09-15 17:34:43 发布
关键词由CSDN通过智能技术生成