山西建设网站的公司,公司网站上线,黄龙云 加强网站建设,wordpress拖拽式建站主题大家好#xff0c;我是雄雄#xff0c;欢迎关注微信公众号#xff1a;雄雄的小课堂 今天分享的是#xff1a;在java中#xff0c;根据指定日期显示出前n天的日期
效果如下#xff1a; 大家注意观察上面的时间#xff0c;我传入的时间是#xff1a;2022年5月9日21:28:… 大家好我是雄雄欢迎关注微信公众号雄雄的小课堂 今天分享的是在java中根据指定日期显示出前n天的日期
效果如下 大家注意观察上面的时间我传入的时间是2022年5月9日21:28:03第二个参数表示前多少天的日期我传入的是7也就是一周。
显示的出来的日期正好是7天的日期代码如下 /*** 根据当前时间获取往前n天的时间*/public static ListString getWeekDateByCurrentDate(Date currentDate,int n) {ListString listDate new ArrayList();SimpleDateFormat dateFormat new SimpleDateFormat(yyyy-MM-dd);try {Calendar calendar Calendar.getInstance();calendar.setTime(currentDate);calendar.add(Calendar.DAY_OF_MONTH, -n);for (int i 0; i n; i) {listDate.add(dateFormat.format(calendar.getTime()));calendar.add(Calendar.DAY_OF_MONTH, 1);}} catch (Exception e) {e.printStackTrace();}return listDate;}调用如下 Testpublic void contextLoads() {/*Long val System.currentTimeMillis();System.out.println(val);*/ListString dateStr DateParseUtils.getWeekDateByCurrentDate(new Date(),7);for (String str : dateStr) {System.out.println(str);}}