14.json 3.2 KB
Newer Older
每日一练社区's avatar
test  
每日一练社区 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
{
   "question_id": 150738,
   "question_title": "对给定的两个日期之间的日期进行遍历",
   "question_content": "对给定的两个日期之间的日期进行遍历,比如startTime 是 2014-07-11;endTime 是 2014-08-11 如何把他们之间的日期获取并遍历出来。",
   "difficulty": "简单",
   "answer_id": 72266,
   "answer_content": "import java.text.ParseException;\nimport java.text.SimpleDateFormat;\nimport java.util.ArrayList;\nimport java.util.Date;\nimport java.util.List;\n\npublic class SplitTime {\n\n\tprivate static List<Date> dateSplit(Date startDate, Date endDate)\n\t\t\tthrows 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\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()\n\t\t\t\t\t- (24 * 60 * 60 * 1000)));// 比上一天减一\n\t\t}\n\t\treturn dateList;\n\t}\n\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\n}\n运行结果:\n2015-05-02\n2015-05-01\n2015-04-30\n2015-04-29\n2015-04-28\n2015-04-27\n2015-04-26\n2015-04-25\n2015-04-24\n2015-04-23\n2015-04-22\n2015-04-21\n2015-04-20",
   "tag_name": "java",
   "java": "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}",
   "topic_link": "https://bbs.csdn.net/topics/600470283",
   "status": 1,
   "keywords": "遍历",
   "license": "csdn.net",
   "notebook": {
      "java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/java/14.ipynb?type=file"
   },
   "notebook_enable": 1
}