当前位置:网站首页>获取当前时间的前N天和前后天的数组列表循环遍历每一天
获取当前时间的前N天和前后天的数组列表循环遍历每一天
2022-07-27 14:48:00 【白马镇的少年】
最近项目中出现一个需求,当前日期的前后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; }
边栏推荐
- DEX and AMMS of DFI security
- Characters generated by JMeter function assistant in jmeter5.3 and later versions cannot be copied when they are grayed out
- 大数相加
- COMS Technology
- solidwork装配体导入到Adams中出现多个Part重名和Part丢失的情况处理
- 嵌入式面试
- Snowflake ID (go Implementation)
- Crmeb Pro v1.4 makes the user experience more brilliant!
- excel skill
- word中插入度的方法
猜你喜欢

C channel simply implements the publishing and subscription of message queue

MySQL index

Solve the problem that Flink cannot be closed normally after startup

*List reversal

The 21st - -- the 30th time

The class declared by the scala object keyword directly calls methods and associated objects

Exe program encryption lock

201403-1

Implementation of filler creator material editing tool

雪花ID(Go 实现)
随机推荐
爬取常见英文名
Addition of large numbers
For enterprise operation and maintenance security, use the cloud housekeeper fortress machine!
Crmeb Pro v1.4 makes the user experience more brilliant!
OpenCV(一)——图像基础知识
2021-03-09
SolidWorks Simulation曲线图属性设定
Replication of complex linked list
gpt-2 文本生成
大数相加
ADAMS中转动整个模型
将IAR工程文件夹转移目录后无法进入函数定义解决
Pointer summary
Matlab legend usage
Opencv (II) -- basic image processing
CCF-201312-1
DRF learning notes (I): Data Serialization
SolidWorks simulation curve attribute setting
Clear understanding of torchvision (mind map)
The method of inserting degree in word