/*** 获取一段时间内的每一天* @param beginDate* @param endDate* @return*/public static List<String> getEveryDay(String beginDate,String endDate) {final Calendar c = Calendar.getInstance();c.clear();if (beginDate.indexOf("-") == -1 || endDate.indexOf("-") == -1 ) {return Collections.emptyList();}String[] split = beginDate.split("-");String[] endsplit = endDate.split("-");if (split.length < 3 || endsplit.length < 3) {return Collections.emptyList();}int start_year = 0;int startMonth = 0;int startDate = 0;Date eDate = null;try {start_year = Integer.valueOf(split[0]);startMonth = Integer.valueOf(split[1]);startDate = Integer.valueOf(split[2]);eDate = formatDate.parse(endDate);Date bDate= formatDate.parse(beginDate);if (eDate.getTime() - bDate.getTime() < 0) {return Collections.emptyList();}} catch (NumberFormatException e) {e.printStackTrace();return Collections.emptyList();} catch (ParseException e) {e.printStackTrace();return Collections.emptyList();}ArrayList<String> resultMap = new ArrayList<>();for (c.set(start_year, startMonth-1, startDate,0,0,0); c.get(Calendar.YEAR) >= start_year; c.add(Calendar.DATE, 1)) {long timeInMillis = c.getTimeInMillis();Date time = c.getTime();TimeZone timeZone = c.getTimeZone();long time1 = c.getTime().getTime();long time2 = eDate.getTime();String date = formatDate.format(timeInMillis);resultMap.add(date);if (c.getTimeInMillis() >= eDate.getTime()) {break;}}return resultMap;}//例子public static void main(String[] args) {List<String> everyDay = getEveryDay("2018-12-31", "2019-2-1");System.out.println(everyDay);}
详细解决方案
获取一段时间内的每一天(实现两个日期之间的所有日期遍历)
热度:14 发布时间:2024-02-19 14:53:52.0
相关解决方案