{
  "type": "code_options",
  "author": "csdn.net",
  "source": "solution.md",
  "exercise_id": "959f627ef9dc4756b005ec647428533d",
  "keywords": "遍历",
  "title": "对给定的两个日期之间的日期进行遍历",
  "desc": [
    {
      "content": "\n对给定的两个日期之间的日期进行遍历,比如startTime 是 2014-07-11；endTime 是 2014-08-11 如何把他们之间的日期获取并遍历出来。",
      "language": "markdown"
    }
  ],
  "answer": [
    {
      "content": "",
      "language": "java"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "java"
      }
    ],
    [
      {
        "content": "",
        "language": "java"
      }
    ],
    [
      {
        "content": "",
        "language": "java"
      }
    ]
  ],
  "template": {
    "content": "import java.text.ParseException;\nimport java.text.SimpleDateFormat;\nimport java.util.ArrayList;\nimport java.util.Date;\nimport java.util.List;\npublic class SplitTime {\n\tprivate static List<Date> dateSplit(Date startDate, Date endDate) throws Exception {\n\t\tif (!startDate.before(endDate))\n\t\t\tthrow new Exception(\"开始时间应该在结束时间之后\");\n\t\tLong spi = endDate.getTime() - startDate.getTime();\n\t\tLong step = spi / (24 * 60 * 60 * 1000);\n\t\tList<Date> dateList = new ArrayList<Date>();\n\t\tdateList.add(endDate);\n\t\tfor (int i = 1; i <= step; i++) {\n\t\t\tdateList.add(new Date(dateList.get(i - 1).getTime() - (24 * 60 * 60 * 1000)));\n\t\t}\n\t\treturn dateList;\n\t}\n\tpublic static void main(String[] args) throws ParseException {\n\t\ttry {\n\t\t\tSimpleDateFormat sdf = new SimpleDateFormat(\"yyyy-MM-dd\");\n\t\t\tDate start = sdf.parse(\"2015-4-20\");\n\t\t\tDate end = sdf.parse(\"2015-5-2\");\n\t\t\tList<Date> lists = dateSplit(start, end);\n\t\t\tif (!lists.isEmpty()) {\n\t\t\t\tfor (Date date : lists) {\n\t\t\t\t\tSystem.out.println(sdf.format(date));\n\t\t\t\t}\n\t\t\t}\n\t\t} catch (Exception e) {\n\t\t}\n\t}\n}",
    "language": "java"
  },
  "node_id": "dailycode-4756dcc742414555aafd768d88b1b5a0",
  "license": "csdn.net",
  "created_at": 1637894161,
  "topic_link": "https://bbs.csdn.net/topics/600470283"
}