{"id":954,"date":"2018-09-17T18:48:48","date_gmt":"2018-09-17T09:48:48","guid":{"rendered":"https:\/\/chat-messenger.com\/?p=954"},"modified":"2018-12-23T01:52:35","modified_gmt":"2018-12-22T16:52:35","slug":"calculating-dates","status":"publish","type":"post","link":"https:\/\/chat-messenger.com\/en\/blog\/java\/calculating-dates","title":{"rendered":"Java Date\/Time Calculation Addition and Subtraction Made Easy"},"content":{"rendered":"<h2 class=\"common_title\"><a name=\"0\">Java <strong>Date and time calculations<\/strong>\u3000Easy addition and subtraction<\/a><\/h2>\n<p>\n\u3000Easily with Java<strong>Calculate addition and subtraction of date and time<\/strong>The following is a sample program that does the following<br \/>The features of the sample program are as follows<\/p>\n<ul>\n<li>Returns the result of adding or subtracting a specified amount of time from the current or any date or time.<\/li>\n<li>If a positive number is specified, time moves forward; if a negative number is specified, time moves backward.<br \/>\nFor example, if you want to set the date 10 days before the current date\/time to<strong>calculation<\/strong>The following is a list of the cases where<br \/>\n<b>Calendar cal = addMonth(-10);<\/b>\n<\/li>\n<li>Any amount of time can be set for the year, month, day, hour, minute, second, and millisecond time fields.<br \/>\nAlso, relative to the specified time<strong>calculation<\/strong>If you want to do so, pass the Calendar class as an argument.<br \/>\nFor example, the following will advance one year from the specified Calendar instance.<br \/>\n<b>Calendar cal = add(\"specified Calendar\",1,0,0,0,0,0,0);<\/b>\n<\/li>\n<li>If the value of each time field exceeds its range, the next larger time field is incremented or decremented.<br \/>\nFor example, the following will advance the current time by one hour and five minutes.<br \/>\n<b>Calendar cal = addMinute(65);<\/b>\n<\/li>\n<\/ul>\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\">sample program<\/a><\/h2>\n<p><textarea readonly=\"readonly\" style=\"font-size: 13px;height: 400px;\" class=\"src\" onclick=\"this.focus();this.select()\">\/**<br \/>\n * \u73fe\u5728\u306e\u65e5\u4ed8\u30fb\u6642\u523b\u304b\u3089\u6307\u5b9a\u306e\u3010\u5e74\u6570\u3011\u3092\u52a0\u7b97\u30fb\u6e1b\u7b97\u3057\u305f\u7d50\u679c\u3092\u8fd4\u3057\u307e\u3059\u3002<br \/>\n * @param addYera \u52a0\u7b97\u30fb\u6e1b\u7b97\u3059\u308b\u5e74\u6570<br \/>\n * @return    \u8a08\u7b97\u5f8c\u306e Calendar \u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3002<br \/>\n *\/<br \/>\npublic static Calendar addYera(int addYera){<br \/>\n    return add(null,addYera,0,0,0,0,0,0);<br \/>\n}<br \/>\n\/**<br \/>\n * \u73fe\u5728\u306e\u65e5\u4ed8\u30fb\u6642\u523b\u304b\u3089\u6307\u5b9a\u306e\u3010\u6708\u6570\u3011\u3092\u52a0\u7b97\u30fb\u6e1b\u7b97\u3057\u305f\u7d50\u679c\u3092\u8fd4\u3057\u307e\u3059\u3002<br \/>\n * @param addMonth \u52a0\u7b97\u30fb\u6e1b\u7b97\u3059\u308b\u6708\u6570<br \/>\n * @return    \u8a08\u7b97\u5f8c\u306e Calendar \u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3002<br \/>\n *\/<br \/>\npublic static Calendar addMonth(int addMonth){<br \/>\n    return add(null,0,addMonth,0,0,0,0,0);<br \/>\n}<br \/>\n\/**<br \/>\n * \u73fe\u5728\u306e\u65e5\u4ed8\u30fb\u6642\u523b\u304b\u3089\u6307\u5b9a\u306e\u3010\u65e5\u6570\u3011\u3092\u52a0\u7b97\u30fb\u6e1b\u7b97\u3057\u305f\u7d50\u679c\u3092\u8fd4\u3057\u307e\u3059\u3002<br \/>\n * @param addDate \u52a0\u7b97\u30fb\u6e1b\u7b97\u3059\u308b\u65e5\u6570<br \/>\n * @return    \u8a08\u7b97\u5f8c\u306e Calendar \u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3002<br \/>\n *\/<br \/>\npublic static Calendar addDate(int addDate){<br \/>\n    return add(null,0,0,addDate,0,0,0,0);<br \/>\n}<br \/>\n\/**<br \/>\n * \u73fe\u5728\u306e\u65e5\u4ed8\u30fb\u6642\u523b\u304b\u3089\u6307\u5b9a\u306e\u3010\u6642\u9593\u3011\u3092\u52a0\u7b97\u30fb\u6e1b\u7b97\u3057\u305f\u7d50\u679c\u3092\u8fd4\u3057\u307e\u3059\u3002<br \/>\n * @param addHour \u52a0\u7b97\u30fb\u6e1b\u7b97\u3059\u308b\u6642\u9593<br \/>\n * @return    \u8a08\u7b97\u5f8c\u306e Calendar \u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3002<br \/>\n *\/<br \/>\npublic static Calendar addHour(int addHour){<br \/>\n    return add(null,0,0,0,addHour,0,0,0);<br \/>\n}<br \/>\n\/**<br \/>\n * \u73fe\u5728\u306e\u65e5\u4ed8\u30fb\u6642\u523b\u304b\u3089\u6307\u5b9a\u306e\u3010\u5206\u3011\u3092\u52a0\u7b97\u30fb\u6e1b\u7b97\u3057\u305f\u7d50\u679c\u3092\u8fd4\u3057\u307e\u3059\u3002<br \/>\n * @param addMinute \u52a0\u7b97\u30fb\u6e1b\u7b97\u3059\u308b\u5206<br \/>\n * @return    \u8a08\u7b97\u5f8c\u306e Calendar \u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3002<br \/>\n *\/<br \/>\npublic static Calendar addMinute(int addMinute){<br \/>\n    return add(null,0,0,0,0,addMinute,0,0);<br \/>\n}<br \/>\n\/**<br \/>\n * \u73fe\u5728\u306e\u65e5\u4ed8\u30fb\u6642\u523b\u304b\u3089\u6307\u5b9a\u306e\u3010\u79d2\u3011\u3092\u52a0\u7b97\u30fb\u6e1b\u7b97\u3057\u305f\u7d50\u679c\u3092\u8fd4\u3057\u307e\u3059\u3002<br \/>\n * @param addSecond \u52a0\u7b97\u30fb\u6e1b\u7b97\u3059\u308b\u79d2<br \/>\n * @return    \u8a08\u7b97\u5f8c\u306e Calendar \u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3002<br \/>\n *\/<br \/>\npublic static Calendar addSecond(int addSecond){<br \/>\n    return add(null,0,0,0,0,0,addSecond,0);<br \/>\n}<br \/>\n\/**<br \/>\n * \u73fe\u5728\u306e\u65e5\u4ed8\u30fb\u6642\u523b\u304b\u3089\u6307\u5b9a\u306e\u6642\u9593\u91cf\u3092\u52a0\u7b97\u30fb\u6e1b\u7b97\u3057\u305f\u7d50\u679c\u3092\u8fd4\u3057\u307e\u3059\u3002<br \/>\n * \u5e74\u3001\u6708\u3001\u65e5\u3001\u6642\u9593\u3001\u5206\u3001\u79d2\u3001\u30df\u30ea\u79d2\u306e\u5404\u6642\u9593\u30d5\u30a3\u30fc\u30eb\u30c9\u306b\u5bfe\u3057\u3001<br \/>\n * \u4efb\u610f\u306e\u6642\u9593\u91cf\u3092\u8a2d\u5b9a\u3067\u304d\u307e\u3059\u3002<br \/>\n * \u305f\u3068\u3048\u3070\u3001\u73fe\u5728\u306e\u65e5\u4ed8\u6642\u523b\u304b\u3089 10 \u65e5\u524d\u3092\u8a08\u7b97\u3059\u308b\u5834\u5408\u306f\u4ee5\u4e0b\u3068\u306a\u308a\u307e\u3059\u3002<br \/>\n * Calendar cal = add(null,0,0,-10,0,0,0,0);<br \/>\n *<br \/>\n * \u5404\u6642\u9593\u30d5\u30a3\u30fc\u30eb\u30c9\u306e\u5024\u304c\u305d\u306e\u7bc4\u56f2\u3092\u8d85\u3048\u305f\u5834\u5408\u3001\u6b21\u306e\u5927\u304d\u3044\u6642\u9593\u30d5\u30a3\u30fc\u30eb\u30c9\u304c<br \/>\n * \u5897\u5206\u307e\u305f\u306f\u6e1b\u5206\u3055\u308c\u307e\u3059\u3002<br \/>\n * \u305f\u3068\u3048\u3070\u3001\u4ee5\u4e0b\u3067\u306f1\u6642\u9593\u30685\u5206\u9032\u3081\u308b\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002<br \/>\n * Calendar cal = add(null,0,0,0,0,65,0,0);<br \/>\n *<br \/>\n * \u5404\u6642\u9593\u30d5\u30a3\u30fc\u30eb\u30c9\u306b\u8a2d\u5b9a\u3059\u308b\u6570\u91cf\u304c0\u306e\u5834\u5408\u306f\u3001\u73fe\u5728\u306e\u5024\u304c\u8a2d\u5b9a\u3055\u308c\u307e\u3059\u3002<br \/>\n * java.util.GregorianCalendar\u306e\u5185\u90e8\u51e6\u7406\u3067\u306f\u4ee5\u4e0b\u306e\u5206\u5c90\u3092\u884c\u3063\u3066\u3044\u308b\u3002<br \/>\n *     if (amount == 0) {<br \/>\n *         return;<br \/>\n *     }<br \/>\n *<br \/>\n * @param cal \u65e5\u4ed8\u6642\u523b\u306e\u6307\u5b9a\u304c\u3042\u308c\u3070\u30bb\u30c3\u30c8\u3059\u308b\u3002<br \/>\n *     null\u306e\u5834\u5408\u3001\u73fe\u5728\u306e\u65e5\u4ed8\u6642\u523b\u3067\u65b0\u3057\u3044Calendar\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u751f\u6210\u3059\u308b\u3002<br \/>\n * @param addYera \u52a0\u7b97\u30fb\u6e1b\u7b97\u3059\u308b\u5e74\u6570<br \/>\n * @param addMonth \u52a0\u7b97\u30fb\u6e1b\u7b97\u3059\u308b\u6708\u6570<br \/>\n * @param addDate \u52a0\u7b97\u30fb\u6e1b\u7b97\u3059\u308b\u65e5\u6570<br \/>\n * @param addHour \u52a0\u7b97\u30fb\u6e1b\u7b97\u3059\u308b\u6642\u9593<br \/>\n * @param addMinute \u52a0\u7b97\u30fb\u6e1b\u7b97\u3059\u308b\u5206<br \/>\n * @param addSecond \u52a0\u7b97\u30fb\u6e1b\u7b97\u3059\u308b\u79d2<br \/>\n * @param addMillisecond \u52a0\u7b97\u30fb\u6e1b\u7b97\u3059\u308b\u30df\u30ea\u79d2<br \/>\n * @return    \u8a08\u7b97\u5f8c\u306e Calendar \u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3002<br \/>\n *\/<br \/>\npublic static Calendar add(Calendar cal,<br \/>\n                           int addYera,int addMonth,int addDate,<br \/>\n                           int addHour,int addMinute,int addSecond,<br \/>\n                           int addMillisecond){<br \/>\n    if (cal == null) {<br \/>\n        cal = Calendar.getInstance();<br \/>\n    }<br \/>\n    cal.add(Calendar.YEAR, addYera);<br \/>\n    cal.add(Calendar.MONTH, addMonth);<br \/>\n    cal.add(Calendar.DATE, addDate);<br \/>\n    cal.add(Calendar.HOUR_OF_DAY, addHour);<br \/>\n    cal.add(Calendar.MINUTE, addMinute);<br \/>\n    cal.add(Calendar.SECOND, addSecond);<br \/>\n    cal.add(Calendar.MILLISECOND, addMillisecond);<br \/>\n    return cal;<br \/>\n}<br \/>\n<\/textarea><br \/>\n<\/p>\n<h2 class=\"common_title\"><a name=\"3\">execution (e.g. program)<\/a><\/h2>\n<p>\n\u3000In the sample program, the return value is<strong>after calculation<\/strong>Calendar object that holds the date of the date, so it is converted to a java.util.Date by the java.util.Calendar#getTime() method, and then converted to a date string by java.text.SimpleDateFormat. SimpleDateFormat.\n<\/p>\n<p>\u25c6Example of Execution<\/p>\n<pre class=\"sample_src\">public static void main(String[] args) {\r\n\r\nSimpleDateFormat f = new SimpleDateFormat(\"yyyy\/MM\/dd HH:mm:ss\");\r\nSystem.out.println(\"Current date\/time = \"+ f.format(add(null,0,0,0,0,0,0,0,0).getTime()));\r\nSystem.out.println(\"Date\/time 1 year later = \"+ f.format(addYera(1).getTime())));\r\nSystem.out.println(\"date\/time 1 month ago = \"+ f.format(addMonth(-1).getTime())));\r\nSystem.out.println(\"date\/time 3 hours later = \"+ f.format(addHour(3).getTime())));\r\nSystem.out.println(\"Date\/time 30 hours ago = \"+ f.format(addHour(-30).getTime())));\r\nSystem.out.println(\"Date\/time 40 minutes later = \"+ f.format(addMinute(40).getTime())));\r\nSystem.out.println(\"Date\/time 80 seconds later = \"+ f.format(addSecond(80).getTime())));\r\nSystem.out.println(\"Date\/time from 1 month ago, 4 days later = \"+ f.format(add(null,0,-1,4,0,0,0,0,0).getTime()));\r\n\r\n}\r\n<\/pre>\n<p>\u25c6Execution Result<br \/>\nThe current date and time is set to 2008\/03\/25 00:00:00.<\/p>\n<pre class=\"console\">Current date\/time = 2008\/03\/25 00:00:00\r\nDate\/Time one year from now = 2009\/03\/25 00:00:00\r\nDate\/Time 1 month ago = 2008\/02\/25 00:00:00\r\n3 hours later date\/time = 2008\/03\/25 03:00:00\r\nDate\/Time 30 hours ago = 2008\/03\/23 18:00:00\r\nDate\/Time 40 minutes later = 2008\/03\/25 00:40:00\r\nDate\/Time 80 seconds later = 2008\/03\/25 00:01:20\r\nDate and time 4 days from 1 month ago = 2008\/02\/29 00:00:00\r\n<\/pre>\n<p>*Because 2008 is a leap year, the last day of February is the 29th.<\/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 Date\/Time Calculations Easy Addition\/Subtraction A sample program for easily calculating addition\/subtraction of dates\/times in Java [\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\/954"}],"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=954"}],"version-history":[{"count":6,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/posts\/954\/revisions"}],"predecessor-version":[{"id":1877,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/posts\/954\/revisions\/1877"}],"wp:attachment":[{"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/media?parent=954"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/categories?post=954"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/tags?post=954"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}