要求:
时间:2019-01-01~~~~2019-01-05 取出改时间段内的具体天数。
private static DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd", Locale.CHINA);
/** * 两个时间点的天数 * 参数毕传 * * @param starDate yyyy-MM-dd HH:mm * @param endDate yyyy-MM-dd HH:mm * @return */ public static List<String> getDateTime(String starDate, String endDate) { List<String> list = new ArrayList<>(); // 开始--结束 LocalDate localDate = LocalDate.parse(starDate.substring(0, 10), dateFormatter); long startTime = localDate.atStartOfDay().toInstant(ZoneOffset.of("+8")).toEpochMilli(); LocalDate endLocalDate = LocalDate.parse(endDate.substring(0, 10), dateFormatter); long endTime = endLocalDate.atStartOfDay().toInstant(ZoneOffset.of("+8")).toEpochMilli(); while (startTime <= endTime) { String str = dateFormatter.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(startTime), ZoneId.of("Asia/Shanghai"))); list.add(str); startTime += (24 * 60 * 60 * 1000); } return list; }
测试:
public static void main(String[] args) { String starttime="2019-01-01"; String endtime="2019-01-05"; List<String> dateTime = DateUtil.getDateTime(starttime, endtime); System.out.println(JSONObject.toJSONString(dateTime)); }
结果: