{"id":951,"date":"2018-09-17T18:45:12","date_gmt":"2018-09-17T09:45:12","guid":{"rendered":"https:\/\/chat-messenger.com\/?p=951"},"modified":"2018-12-23T01:52:45","modified_gmt":"2018-12-22T16:52:45","slug":"difference-month","status":"publish","type":"post","link":"https:\/\/chat-messenger.com\/zh\/\u535a\u5ba2\/java\/\u5dee\u6708","title":{"rendered":"Java \u627e\u51fa\u4e24\u4e2a\u65e5\u671f\u4e4b\u95f4\u7684\u6708\u6570\u5dee"},"content":{"rendered":"<h2 class=\"common_title\"><a name=\"0\">Java \u627e\u51fa\u4e24\u4e2a\u65e5\u671f\u4e4b\u95f4\u7684\u6708\u6570\u5dee<\/a><\/h2>\n<p>\n\u5728 Java \u4e2d\uff0c\u6709\u4e24\u4e2a<strong>\u65e5\u671f\u7684\u6708\u6570\u5dee\u5f02<\/strong>\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u7a0b\u5e8f\uff0c\u7528\u4e8e\u67e5\u627e<br \/>\n\u793a\u4f8b\u7a0b\u5e8f\u652f\u6301\u8981\u6bd4\u8f83\u7684\u65e5\u671f\u4e3a\u5b57\u7b26\u4e32\uff08yyyy\/MM\/dd\uff09\u7684\u60c5\u51b5\u3002\n<\/p>\n<p><strong>\u6708\u6570\u5dee\u5f02<\/strong>\u7528\u4e8e\u786e\u5b9a<\/p>\n<ol>\n<li>\u9996\u5148\uff0c\u4e24\u4e2a\u65e5\u671f\u5b9a\u5728\u4e00\u5929\u3002<br \/>\n    * Calendar.set(Calendar.DATE,1)\u3002<\/li>\n<li>\u7136\u540e\u9010\u6708\u53cd\u590d\u6bd4\u8f83\u8fd9\u4e24\u4e2a\u65e5\u671f\u3002<br \/>\n    * \u4f7f\u7528 Calendar.add(Calendar.MONTH,1 \u6216-1)\uff0c\u4e00\u6b21\u589e\u52a0\u6216\u51cf\u5c11\u4e00\u4e2a\u6708\u3002<\/li>\n<li>\u8ba1\u7b97\u91cd\u590d\u6bd4\u8f83\u7684\u6b21\u6570\u3002<\/li>\n<\/ol>\n<p><script type=\"text\/javascript\" src=\"https:\/\/chat-messenger.com\/js\/common.js?dd12sssas2223\" charset=\"UTF-8\"><\/script><br \/>\n<script type=\"text\/javascript\"> writePR(); <\/script><\/p>\n<h2 class=\"common_title\"><a name=\"1\">\u6837\u672c\u6e90<\/a><\/h2>\n<p><textarea readonly=\"readonly\" style=\"font-size: 13px;height: 400px;\" class=\"src\" onclick=\"this.focus();this.select()\">\/**<br \/>\n * 2\u3064\u306e\u65e5\u4ed8\u306e\u6708\u6570\u306e\u5dee\u3092\u6c42\u3081\u307e\u3059\u3002<br \/>\n * \u65e5\u4ed8\u6587\u5b57\u5217 strDate1 &#8211; strDate2 \u304c\u4f55\u30f5\u6708\u304b\u3092\u6574\u6570\u3067\u8fd4\u3057\u307e\u3059\u3002<br \/>\n * \u203b\u7aef\u6570\u306e\u65e5\u6570\u306f\u7121\u8996\u3057\u307e\u3059\u3002<br \/>\n *<br \/>\n * @param strDate1    \u65e5\u4ed8\u6587\u5b57\u52171    yyyy\/MM\/dd<br \/>\n * @param strDate2    \u65e5\u4ed8\u6587\u5b57\u52172    yyyy\/MM\/dd<br \/>\n * @return 2\u3064\u306e\u65e5\u4ed8\u306e\u6708\u6570\u306e\u5dee<br \/>\n * @throws ParseException \u65e5\u4ed8\u30d5\u30a9\u30fc\u30de\u30c3\u30c8\u304c\u4e0d\u6b63\u306a\u5834\u5408<br \/>\n *\/<br \/>\npublic static int differenceMonth(String strDate1, String strDate2)<br \/>\n    throws ParseException {<br \/>\n    Date date1 = DateFormat.getDateInstance().parse(strDate1);<br \/>\n    Date date2 = DateFormat.getDateInstance().parse(strDate2);<br \/>\n    return differenceMonth(date1,date2);<br \/>\n}<br \/>\n\/**<br \/>\n * 2\u3064\u306e\u65e5\u4ed8\u306e\u6708\u6570\u306e\u5dee\u3092\u6c42\u3081\u307e\u3059\u3002<br \/>\n * java.util.Date \u578b\u306e\u65e5\u4ed8 date1 &#8211; date2 \u304c\u4f55\u30f5\u6708\u304b\u3092\u6574\u6570\u3067\u8fd4\u3057\u307e\u3059\u3002<br \/>\n * \u203b\u7aef\u6570\u306e\u65e5\u6570\u306f\u7121\u8996\u3057\u307e\u3059\u3002<br \/>\n *<br \/>\n * @param date1    \u65e5\u4ed81 java.util.Date<br \/>\n * @param date2    \u65e5\u4ed82 java.util.Date<br \/>\n * @return 2\u3064\u306e\u65e5\u4ed8\u306e\u6708\u6570\u306e\u5dee<br \/>\n *\/<br \/>\npublic static int differenceMonth(Date date1, Date date2) {<br \/>\n    Calendar cal1 = Calendar.getInstance();<br \/>\n    cal1.setTime(date1);<br \/>\n    cal1.set(Calendar.DATE, 1);<br \/>\n    Calendar cal2 = Calendar.getInstance();<br \/>\n    cal2.setTime(date2);<br \/>\n    cal2.set(Calendar.DATE, 1);<br \/>\n    int count = 0;<br \/>\n    if (cal1.before(cal2)) {<br \/>\n        while (cal1.before(cal2)) {<br \/>\n            cal1.add(Calendar.MONTH, 1);<br \/>\n            count&#8211;;<br \/>\n        }<br \/>\n    } else {<br \/>\n        count&#8211;;<br \/>\n        while (!cal1.before(cal2)) {<br \/>\n            cal1.add(Calendar.MONTH, -1);<br \/>\n            count++;<br \/>\n        }<br \/>\n    }<br \/>\n    return count;<br \/>\n}<br \/>\n<\/textarea><\/p>\n<h2 class=\"common_title\"><a name=\"2\">\u6267\u884c<\/a><\/h2>\n<p>\u6267\u884c\u793a\u4f8b<\/p>\n<pre class=\"sample_src\">public static void main(String[] args) {\r\n    int ret = differenceMonth(\"2008\/6\/30\", \"2008\/6\/1\")\uff1b\r\n    System.out.println(\"Result 1 = \"+ret)\uff1b\r\n\r\n    ret = differenceMonth(\"2008\/7\/1\", \"2008\/6\/3\")\uff1b\r\n    System.out.println(\"Result 2 = \"+ret)\uff1b\r\n\r\n    ret = differenceMonth(\"2008\/12\/20\", \"2008\/6\/1\")\uff1b\r\n    System.out.println(\"Result 3 = \"+ret)\uff1b\r\n\r\n    ret = differenceMonth(\"2010\/6\/20\", \"2008\/6\/1\")\uff1b\r\n    System.out.println(\"Result 4 = \"+ret)\uff1b\r\n\r\n    ret = differenceMonth(\"2008\/6\/1\", \"2008\/7\/1\")\uff1b\r\n    System.out.println(\"result 5 = \"+ret)\uff1b\r\n\r\n    ret = differenceMonth(\"2008\/7\/1\", \"2009\/7\/1\")\uff1b\r\n    System.out.println(\"result 6 = \"+ret)\uff1b\r\n}\r\n<\/pre>\n<p>\u6267\u884c\u7ed3\u679c<\/p>\n<pre class=\"console\">\u7ed3\u679c 1 = 0\r\n\u7ed3\u679c 2 = 1\r\n\u7ed3\u679c 3 = 6\r\n\u7ed3\u679c 4 = 24\r\n\u7ed3\u679c 5 = -1\r\n\u7ed3\u679c 6 = -12\r\n<\/pre>\n<p>\n\u8be5\u793a\u4f8b\u7a0b\u5e8f\u6765\u81ea Chat&amp;Messenger \u7684 \"<a href=\"https:\/\/chat-messenger.com\/zh\/\u624b\u518c\/schedule_calendar\/\u884c\u4e8b\u5386\/\" title=\"\u65e5\u5386\u548c\u65e5\u7a0b\u5b89\u6392\u529f\u80fd\">\u65e5\u5386\u548c\u65e5\u7a0b\u5b89\u6392\u529f\u80fd<\/a>\u8be5\u7cfb\u7edf\u5b9e\u9645\u7528\u4e8e\u4ee5\u4e0b\u9886\u57df\u3002<\/p>","protected":false},"excerpt":{"rendered":"<p>Java \u67e5\u627e\u4e24\u4e2a\u65e5\u671f\u4e4b\u95f4\u7684\u6708\u6570\u5dee\u5f02\u4ecb\u7ecd\u4e00\u4e2a Java \u793a\u4f8b\u7a0b\u5e8f\uff0c\u8be5\u7a0b\u5e8f\u53ef\u67e5\u627e\u4e24\u4e2a\u65e5\u671f\u4e4b\u95f4\u7684\u6708\u6570\u5dee\u5f02 [...]<\/p>","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"swell_btn_cv_data":""},"categories":[19],"tags":[],"_links":{"self":[{"href":"https:\/\/chat-messenger.com\/zh\/wp-json\/wp\/v2\/posts\/951"}],"collection":[{"href":"https:\/\/chat-messenger.com\/zh\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/chat-messenger.com\/zh\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/chat-messenger.com\/zh\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/chat-messenger.com\/zh\/wp-json\/wp\/v2\/comments?post=951"}],"version-history":[{"count":4,"href":"https:\/\/chat-messenger.com\/zh\/wp-json\/wp\/v2\/posts\/951\/revisions"}],"predecessor-version":[{"id":1878,"href":"https:\/\/chat-messenger.com\/zh\/wp-json\/wp\/v2\/posts\/951\/revisions\/1878"}],"wp:attachment":[{"href":"https:\/\/chat-messenger.com\/zh\/wp-json\/wp\/v2\/media?parent=951"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chat-messenger.com\/zh\/wp-json\/wp\/v2\/categories?post=951"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chat-messenger.com\/zh\/wp-json\/wp\/v2\/tags?post=951"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}