public static void main(String[] args) {
//获取现在的时间
Date startDateTime = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(startDateTime);
//获取一年前的今天的时间
calendar.add(Calendar.YEAR,-1);
Date endDateTime = calendar.getTime();
//将开始时间和结束时间格式化
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
//格式化开始时间
String startDateTimeFormat = simpleDateFormat.format(startDateTime);
//格式化结束时间
String endDateTimeFormat = simpleDateFormat.format(endDateTime);
List<String> toDay = everyDay(endDateTimeFormat,startDateTimeFormat);
System.out.println(toDay);
}
public static List<String> everyDay(String startDateTime, String endDateTime) {
List<String> list = new ArrayList<>();
//拆分成数组
String[] dateStart = startDateTime.split("-");
String[] dateEnd = endDateTime.split("-");
//开始时间转换成时间戳
Calendar start = Calendar.getInstance();
start.set(Integer.valueOf(dateStart[0]), Integer.valueOf(dateStart[1]) - 1, Integer.valueOf(dateStart[2]));
Long startTIme = start.getTimeInMillis();
//结束时间转换成时间戳
Calendar end = Calendar.getInstance();
end.set(Integer.valueOf(dateEnd[0]), Integer.valueOf(dateEnd[1]) - 1, Integer.valueOf(dateEnd[2]));
Long endTime = end.getTimeInMillis();
//定义一个一天的时间戳时长
Long oneDay = 1000 * 60 * 60 * 24L;
Long time = startTIme;
//循环得出
while (time <= endTime) {
list.add(new SimpleDateFormat("yyyy-MM-dd").format(new Date(time)));
time += oneDay;
}
return list;
}
根据起始时间 获取区间的每一天时间
于 2022-07-25 00:43:54 首次发布