获取当前时间的前N天和前后天的数组列表循环遍历每一天

最近项目中出现一个需求,当前日期的前后N天选择 ,获取日期

import java.text.DateFormat;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;

----------------------------------------------------------------------------------- 

String afterTime;
String fetureTime;
List<String> listDatefan;

viewCreate这个方法里面写下面这段代码

DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
afterTime = df.format(new Date().getTime() - 15 * 24 * 60 * 60 * 1000);//之前15天,
fetureTime = df.format(new Date().getTime() + 15 * 24 * 60 * 60 * 1000);//未来15天
String nowTime = df.format(new Date());//当前时间
handleCirculationDate(fetureTime, afterTime);
binding.tvTime.setSelected(true);

listDatefan = new ArrayList<>();
//反序
Collections.reverse(listDate);
for (String n : listDate) {
    listDatefan.add(n);
}

 

 

 

List<String> listDate;

public List<String> handleCirculationDate(String fetureTime, String afterTime) {
    listDate = new ArrayList<>();
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");//日期格式
    try {
        Date startDate = dateFormat.parse(afterTime);//之前
        Date endDate = dateFormat.parse(fetureTime);//未来
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startDate);//之前的
        while (calendar.getTime().before(endDate)) {
            listDate.add(dateFormat.format(calendar.getTime()));
            calendar.add(Calendar.DAY_OF_MONTH, 1);
        }
        return listDate;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值