/**
* 获取时间区间的所有整点时间
*
* @param startTime 开始时间
* @param endTime 结束时间
* @return 整点时间集合
*/
public List<String> getHourList(String startTime, String endTime) {
List<String> hourList = new ArrayList<>();
SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH");
SimpleDateFormat format3 = new SimpleDateFormat("yyyy-MM-dd HH:00:00");
try {
Date startTime1 = format2.parse(startTime);
Date endTime1 = format2.parse(endTime);
Calendar tempStart = Calendar.getInstance();
tempStart.setTime(startTime1);
while (endTime1.getTime() >= startTime1.getTime()) {
hourList.add(format3.format(startTime1));
//有多种模式可以增加或减去相应的时间
tempStart.add(Calendar.HOUR_OF_DAY, 1);
startTime1 = tempStart.getTime();
}
} catch (ParseException e) {
e.printStackTrace();
}
return hourList;
}
Java获取两个时间内的全部时间
最新推荐文章于 2023-04-26 14:20:50 发布