/** * 从指定开始时间到当前时间的时间列表(每小时一个条目) */ public static List<String> timeListFromStartToNow(){ // 获取当天的日期和时间格式 SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat sdfFull = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 获取当天的日期 String todayDate = sdfDate.format(new Date()); // 定义开始时间的小时部分 String startTimeHour = "01:00:00"; // 组合成完整的开始时间字符串 String startTimeStr = todayDate + " " + startTimeHour; // 解析开始时间 Date startTime = null; try { startTime = sdfFull.parse(startTimeStr); } catch (ParseException e) { throw new RuntimeException(e); } // 获取当前时间作为结束时间(可以根据需要调整) Date endTime = new Date(); // 生成时间列表 List<String> timeList = new ArrayList<>(); Calendar calendar = Calendar.getInstance(); calendar.setTime(startTime); // 循环直到超过endTime while (calendar.getTime().before(endTime)) { timeList.add(sdfFull.format(calendar.getTime())); // 增加一小时 calendar.add(Calendar.HOUR_OF_DAY, 1); } return timeList; }
java获取从指定开始时间到当前时间的时间列表(每小时一个条目)
最新推荐文章于 2025-03-07 16:30:53 发布