{"id":948,"date":"2018-09-17T18:43:54","date_gmt":"2018-09-17T09:43:54","guid":{"rendered":"https:\/\/chat-messenger.com\/?p=948"},"modified":"2019-07-01T00:27:48","modified_gmt":"2019-06-30T15:27:48","slug":"date-difference","status":"publish","type":"post","link":"https:\/\/chat-messenger.com\/en\/blog\/java\/date-difference","title":{"rendered":"Java Find the difference between two dates"},"content":{"rendered":"<h2 class=\"common_title\"><a name=\"0\">Java Find the difference between two dates<\/a><\/h2>\n<p>\nIn Java, two<strong>Date Difference<\/strong>Here is a sample program to find<br \/>\nThe sample program supports the case where the date to be compared is a string ( yyyy\/MM\/dd ), java.util.\n<\/p>\n<p><strong>Date Difference<\/strong>The following is the calculation method for determining<\/p>\n<ol>\n<li>The first two dates are converted to long values.<br \/>\n    This long value is the number of milliseconds elapsed since January 1, 1970 00:00:00 GMT.<\/li>\n<li>Next, find the difference.<\/li>\n<li>The difference in dates can be obtained by dividing the quantity from the above calculation by the hours of the day.<br \/>\n    One day (24 hours) is 86,400,000 milliseconds.<\/li>\n<\/ol>\n<h2 class=\"common_title\"><a name=\"1\">sample source<\/a><\/h2>\n<p><textarea readonly=\"readonly\" style=\"font-size: 13px;height: 600px;\" class=\"src\" onclick=\"this.focus();this.select()\">\/**<br \/>\n * 2\u3064\u306e\u65e5\u4ed8\u306e\u5dee\u3092\u6c42\u3081\u307e\u3059\u3002<br \/>\n * \u65e5\u4ed8\u6587\u5b57\u5217 strDate1 &#8211; strDate2 \u304c\u4f55\u65e5\u304b\u3092\u8fd4\u3057\u307e\u3059\u3002<br \/>\n *<br \/>\n * @param strDate1    \u65e5\u4ed8\u6587\u5b57\u5217 yyyy\/MM\/dd<br \/>\n * @param strDate2    \u65e5\u4ed8\u6587\u5b57\u5217 yyyy\/MM\/dd<br \/>\n * @return    2\u3064\u306e\u65e5\u4ed8\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 differenceDays(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 differenceDays(date1,date2);<br \/>\n}<br \/>\n\/**<br \/>\n * 2\u3064\u306e\u65e5\u4ed8\u306e\u5dee\u3092\u6c42\u3081\u307e\u3059\u3002<br \/>\n * java.util.Date \u578b\u306e\u65e5\u4ed8 date1 &#8211; date2 \u304c\u4f55\u65e5\u304b\u3092\u8fd4\u3057\u307e\u3059\u3002<br \/>\n *<br \/>\n * \u8a08\u7b97\u65b9\u6cd5\u306f\u4ee5\u4e0b\u3068\u306a\u308a\u307e\u3059\u3002<br \/>\n * 1.\u6700\u521d\u306b2\u3064\u306e\u65e5\u4ed8\u3092 long \u5024\u306b\u5909\u63db\u3057\u307e\u3059\u3002<br \/>\n * \u3000\u203b\u3053\u306e long \u5024\u306f 1970 \u5e74 1 \u6708 1 \u65e5 00:00:00 GMT \u304b\u3089\u306e<br \/>\n * \u3000\u7d4c\u904e\u30df\u30ea\u79d2\u6570\u3068\u306a\u308a\u307e\u3059\u3002<br \/>\n * 2.\u6b21\u306b\u305d\u306e\u5dee\u3092\u6c42\u3081\u307e\u3059\u3002<br \/>\n * 3.\u4e0a\u8a18\u306e\u8a08\u7b97\u3067\u51fa\u305f\u6570\u91cf\u3092 1 \u65e5\u306e\u6642\u9593\u3067\u5272\u308b\u3053\u3068\u3067<br \/>\n * \u3000\u65e5\u4ed8\u306e\u5dee\u3092\u6c42\u3081\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<br \/>\n * \u3000\u203b1 \u65e5 ( 24 \u6642\u9593) \u306f\u300186,400,000 \u30df\u30ea\u79d2\u3067\u3059\u3002<br \/>\n *<br \/>\n * @param date1    \u65e5\u4ed8 java.util.Date<br \/>\n * @param date2    \u65e5\u4ed8 java.util.Date<br \/>\n * @return    2\u3064\u306e\u65e5\u4ed8\u306e\u5dee<br \/>\n *\/<br \/>\npublic static int differenceDays(Date date1,Date date2) {<br \/>\n    long datetime1 = date1.getTime();<br \/>\n    long datetime2 = date2.getTime();<br \/>\n    long one_date_time = 1000 * 60 * 60 * 24;<br \/>\n    long diffDays = (datetime1 &#8211; datetime2) \/ one_date_time;<br \/>\n    return (int)diffDays;<br \/>\n}<br \/>\n<\/textarea><\/p>\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=\"2\">execution (e.g. program)<\/a><\/h2>\n<p>\u25c6Example of Execution<\/p>\n<pre class=\"sample_src\">public static void main(String[] args) {\r\n    int ret = differenceDays(\"2008\/6\/2\", \"2008\/6\/1\");\r\n    System.out.println(\"result 1 = \"+ret);\r\n\r\n    ret = differenceDays(\"2008\/6\/1\", \"2008\/6\/3\");\r\n    System.out.println(\"result 2 = \"+ret);\r\n\r\n    \/\/ May has 31 days\r\n    ret = differenceDays(\"2008\/6\/1\", \"2008\/5\/1\");\r\n    System.out.println(\"result 3 = \"+ret);\r\n\r\n    \/\/ 30 days in June\r\n    ret = differenceDays(\"2008\/7\/1\", \"2008\/6\/1\");\r\n    System.out.println(\"result 4 = \"+ret);\r\n\r\n    \/\/ 2008 is a leap year, so the end date of February is the 29th day of the month\r\n    ret = differenceDays(\"2008\/3\/1\", \"2008\/2\/2\/28\");\r\n    System.out.println(\"result 5 = \"+ret);\");\r\n}\r\n<\/pre>\n<p>\u25c6Execution Result<\/p>\n<pre class=\"console\">Result 1 = 1\r\nResult 2 = -2\r\nResult 3 = 31\r\nResult 4 = 30\r\nResult 5 = 2\r\n<\/pre>\n<p>\nFinding the difference between two dates is relatively easy, but finding the difference in months is a bit more challenging. Please see below for reference.<br \/>\n.\"<a href=\"https:\/\/chat-messenger.com\/en\/blog\/java\/difference-month\/\" title=\"Java Find the difference between the number of months between two dates\">Java Find the difference between the number of months between two dates<\/a>\"<\/p>\n<p>This sample program is a Chat&amp;Messenger \"<a href=\"https:\/\/chat-messenger.com\/en\/manual\/schedule_calendar\/calendar\/\" title=\"Calendar and scheduling functions\">Calendar and scheduling functions<\/a>The actual use of the system is done in the \"I'm a fan of this system.<\/p>","protected":false},"excerpt":{"rendered":"<p>Java Find the difference between two dates Introducing a sample program to find the difference between two dates in Java. San [\u2026]<\/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\/en\/wp-json\/wp\/v2\/posts\/948"}],"collection":[{"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/comments?post=948"}],"version-history":[{"count":8,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/posts\/948\/revisions"}],"predecessor-version":[{"id":3655,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/posts\/948\/revisions\/3655"}],"wp:attachment":[{"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/media?parent=948"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/categories?post=948"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/tags?post=948"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}