package com.hzcominfo.hik.hikbigscreen.core; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; public class Test { public static void main(String[] args) throws Exception { List<String> list = addDates("2019-02-01", "2019-02-05"); list.forEach(i -> System.out.println(i) ); } /** * @param cntDateBeg 开始时间 * @param cntDateEnd 结束时间 * @return */ public static List<String> addDates(String cntDateBeg, String cntDateEnd) { List<String> list = new ArrayList<>(); //拆分成数组 String[] dateBegs = cntDateBeg.split("-"); String[] dateEnds = cntDateEnd.split("-"); //开始时间转换成时间戳 Calendar start = Calendar.getInstance(); start.set(Integer.valueOf(dateBegs[0]), Integer.valueOf(dateBegs[1]) - 1, Integer.valueOf(dateBegs[2])); Long startTIme = start.getTimeInMillis(); //结束时间转换成时间戳 Calendar end = Calendar.getInstance(); end.set(Integer.valueOf(dateEnds[0]), Integer.valueOf(dateEnds[1]) - 1, Integer.valueOf(dateEnds[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; } }