java获取过去的7天

at 3周前 ca java by Achin

//获取过去7天

@Test
public void contextLoads() {
// 过去past天
int past = 7;
Calendar calendar = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
ArrayList arrayList = new ArrayList();
for (int i = 0; i 
if (i == 0){
calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - 0);
}else if (i >0){
calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - 1);
}
Date today = calendar.getTime();
String result = format.format(today);
arrayList.add(result);
}
//反转arrayList
Collections.reverse(arrayList);
System.out.print(arrayList+"--------------------");
}
打印7天的日期[2019-12-06, 2019-12-07, 2019-12-08, 2019-12-09, 2019-12-10, 2019-12-11, 2019-12-12]--------------------
//获取未来7天
@Test
public void contextLoads() {
// 未来past天
int past = 7;
Calendar calendar = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
ArrayList arrayList = new ArrayList();
for (int i = 0; i 
if (i == 0){
calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - 0);
}else if (i >0){
calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + 1);
}
Date today = calendar.getTime();
String result = format.format(today);
arrayList.add(result);
}
//反转arrayList 未来past天 就不用反转了 如果要解开注释就好了
//        Collections.reverse(arrayList);
System.out.print(arrayList+"--------------------");
}
打印7天的日期[2019-12-12, 2019-12-13, 2019-12-14, 2019-12-15, 2019-12-16, 2019-12-17, 2019-12-18]--------------------


版权声明

1、本BLOG介绍。此BLOG为个人BLOG,内容均来自原创及互连网转载。最终目的为记录自己需要的内容或自己的学习感悟,不涉及商业用途。

2、本BLOG有关原创文章的版权本BLOG上原创文章未经本人许可,不得用于商业用途及传统媒体。网络媒体转载请注明出处,否则属于侵权行为。

3、本BLOG有关本站侵权本BLOG所转载的内容,均是本人未发现有对文章版权声明的文章且无来自传统媒体。如果本BLOG侵犯了您的权益,请联系本人,本人将会在第一时间删除侵权文章!

4、本站地址:achinblog.cn 联系方式:001@achinblog.cn

已有0条评论