{"id":1128,"date":"2022-10-24T22:45:54","date_gmt":"2022-10-24T14:45:54","guid":{"rendered":"https:\/\/qaqaq.top\/?p=1128"},"modified":"2022-11-27T12:39:49","modified_gmt":"2022-11-27T04:39:49","slug":"%e5%b8%b8%e7%94%a8%e7%b1%bb-simpledateformat%e7%9a%84%e4%bd%bf%e7%94%a8%e3%80%81simpledateformat%e7%9a%84%e8%af%be%e5%90%8e%e7%bb%83%e4%b9%a01%e3%80%81simpledateformat%e7%9a%84%e8%af%be%e5%90%8e","status":"publish","type":"post","link":"https:\/\/qaqaq.top\/?p=1128","title":{"rendered":"\u5e38\u7528\u7c7b-SimpleDateFormat\u7684\u4f7f\u7528\u3001SimpleDateFormat\u7684\u8bfe\u540e\u7ec3\u4e601\u3001SimpleDateFormat\u7684\u8bfe\u540e\u7ec3\u4e602\u3001Calendar\u65e5\u5386\u7c7b\u7684\u4f7f\u7528"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>package top.qaqaq.java.P480;\r\n\r\nimport org.junit.jupiter.api.Test;\r\n\r\nimport java.text.ParseException;\r\nimport java.text.SimpleDateFormat;\r\nimport java.util.Calendar;\r\nimport java.util.Date;\r\n\r\n\/**\r\n * jdk 8\u4e4b\u524d\u7684\u65e5\u671f\u65f6\u95f4\u7684API\u6d4b\u8bd5\r\n * 1. System\u7c7b\u4e2dcurrentTimeMillis();\r\n * 2. java.util.Date\u548c\u5b50\u7c7bjava.sql.Date\r\n * 3. SimpleDateFormat\r\n * 4. Calendar\r\n *\r\n * @author RichieZhang\r\n * @create 2022-10-23 \u4e0b\u5348 4:54\r\n *\/\r\npublic class DateTimeTest {\r\n    \/*\r\n    SimpleDateFormat\u7684\u4f7f\u7528\uff1aSimpleDateFormat\u5bf9\u65e5\u671fDate\u7c7b\u7684\u683c\u5f0f\u5316\u548c\u89e3\u6790\r\n\r\n    1. \u4e24\u4e2a\u64cd\u4f5c\uff1a\r\n    1.1 \u683c\u5f0f\u5316\uff1a\u65e5\u671f ---> \u5b57\u7b26\u4e32\r\n    1.2 \u89e3\u6790\uff1a\u683c\u5f0f\u5316\u7684\u9006\u8fc7\u7a0b\uff0c\u5b57\u7b26\u4e32 ---> \u65e5\u671f\r\n\r\n    2. SimpleDateFormat\u7684\u5b9e\u4f8b\u5316\r\n     *\/\r\n\r\n    @Test\r\n    public void testSimpleDateFormat() throws ParseException {\r\n\r\n        \/\/\u5b9e\u4f8b\u5316SimpleDateFormat:\u4f7f\u7528\u9ed8\u8ba4\u7684\u6784\u9020\u5668\r\n        SimpleDateFormat sdf = new SimpleDateFormat();\r\n\r\n        \/\/\u683c\u5f0f\u5316\u65e5\u671f\uff1a\u65e5\u671f ---> \u5b57\u7b26\u4e32\r\n        Date date = new Date();\r\n        System.out.println(date);\r\n\r\n        String format = sdf.format(date);\r\n        System.out.println(format);\r\n\r\n        \/\/\u89e3\u6790\uff1a\u683c\u5f0f\u5316\u7684\u9006\u8fc7\u7a0b\uff0c\u5b57\u7b26\u4e32 ---> \u65e5\u671f\r\n        String str = \"2022\/10\/23 \u4e0b\u53485:03\";\r\n        Date date1 = sdf.parse(str);\r\n        System.out.println(date1);\r\n\r\n        \/\/**********\u6309\u7167\u6307\u5b9a\u7684\u65b9\u5f0f\u683c\u5f0f\u5316\u548c\u89e3\u6790\uff1a\u8c03\u7528\u5e26\u53c2\u7684\u6784\u9020\u5668**********************\r\n\/\/        SimpleDateFormat sdf1 = new SimpleDateFormat(\"yyyyy.MMMMM.dd GGG hh:mm aaa\");\r\n        SimpleDateFormat sdf1 = new SimpleDateFormat(\"yyyy-MM-dd hh:mm:ss\");\r\n        \/\/\u683c\u5f0f\u5316\r\n        String format1 = sdf1.format(date);\r\n        System.out.println(format1);\/\/2022-10-23 05:14:47\r\n        \/\/\u89e3\u6790:\u8981\u6c42\u5b57\u7b26\u4e32\u5fc5\u987b\u662f\u7b26\u5408SimpleDateFormat\u8bc6\u522b\u7684\u683c\u5f0f\uff08\u901a\u8fc7\u6784\u9020\u5668\u53c2\u6570\u4f53\u73b0\uff09,\r\n        \/\/\u5426\u5219\uff0c\u629b\u5f02\u5e38\r\n        Date date2 = sdf1.parse(\"2022-10-23 05:14:47\");\r\n        System.out.println(date2);\r\n\r\n    }\r\n\r\n    \/*\r\n    \u7ec3\u4e60\u4e00\uff1a\u5b57\u7b26\u4e32\"2020-09-08\"\u8f6c\u6362\u4e3ajava.sql.Date\r\n\r\n    \u7ec3\u4e60\u4e8c\uff1a\"\u4e09\u5929\u6253\u9c7c\u4e24\u5929\u6652\u7f51\" 1990-01-01 xxxx-xx-xx \u6253\u9c7c\uff1f\u6652\u7f51\uff1f\r\n\r\n    \u4e3e\u4f8b\uff1a2020-09-08 \uff1f \u603b\u5929\u6570\r\n\r\n    \u603b\u5929\u6570 % 5 == 1\uff0c2\uff0c3 \uff1a\u6253\u6e14\r\n    \u603b\u5929\u6570 % 5 == 4, 0 \uff1a\u6652\u7f51\r\n\r\n    \u603b\u5929\u6570\u7684\u8ba1\u7b97\uff1f\r\n    \u65b9\u5f0f\u4e00\uff1a(date2.getTime() - date1.getTime()) \/ (1000 * 60 * 60 * 24) + 1\r\n    \u65b9\u5f0f\u4e8c\uff1a1990-01-01 --> 2019-12-31 --> + 2020-0101 -->2020-09-08\r\n     *\/\r\n    @Test\r\n    public void testExer() throws ParseException {\r\n        String birth = \"2020-09-08\";\r\n\r\n        SimpleDateFormat sdf1 = new SimpleDateFormat(\"yyyy-MM-dd\");\r\n        Date date = sdf1.parse(birth);\r\n\/\/        System.out.println(date);\r\n\r\n        java.sql.Date birthDate = new java.sql.Date(date.getTime());\r\n        System.out.println(birthDate);\r\n\r\n    }\r\n\r\n    \/*\r\n    Calendar\u65e5\u5386\u7c7b(\u62bd\u8c61\u7c7b)\u7684\u4f7f\u7528\r\n\r\n     *\/\r\n\r\n    @Test\r\n    public void testCalender(){\r\n        \/\/1.\u5b9e\u4f8b\u5316\r\n        \/\/\u65b9\u5f0f\u4e00\uff1a\u521b\u5efa\u5176\u5b50\u7c7b\uff08GregorianCalendar\uff09\u7684\u5bf9\u8c61\r\n        \/\/\u65b9\u5f0f\u4e8c\uff1a\u8c03\u7528\u5176\u9759\u6001\u65b9\u6cd5getInstance()\r\n        Calendar calendar = Calendar.getInstance();\r\n        System.out.println(calendar.getClass());\r\n\r\n        \/\/2.\u5e38\u7528\u65b9\u6cd5\r\n        \/\/get()\r\n        int days = calendar.get(Calendar.DAY_OF_MONTH);\r\n        System.out.println(days);\r\n        System.out.println(calendar.get(Calendar.DAY_OF_YEAR));\r\n\r\n        \/\/set()\r\n        \/\/calendar\u53ef\u53d8\u6027\r\n        calendar.set(Calendar.DAY_OF_MONTH,22);\r\n        days = calendar.get(Calendar.DAY_OF_MONTH);\r\n        System.out.println(days);\r\n\r\n        \/\/add()\r\n        calendar.add(Calendar.DAY_OF_MONTH,-3);\r\n        days = calendar.get(Calendar.DAY_OF_MONTH);\r\n        System.out.println(days);\r\n\r\n        \/\/getTime():\u65e5\u5386\u7c7b ---> Date\r\n        Date date = calendar.getTime();\r\n        System.out.println(date);\r\n\r\n        \/\/setTime():Date ---> \u65e5\u5386\u7c7b\r\n        Date date1 = new Date();\r\n        calendar.setTime(date1);\r\n        days = calendar.get(Calendar.DAY_OF_MONTH);\r\n        System.out.println(days);\r\n\r\n    }\r\n}\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[46],"class_list":["post-1128","post","type-post","status-publish","format-standard","hentry","category-java-code","tag-java"],"_links":{"self":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1128"}],"collection":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1128"}],"version-history":[{"count":1,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1128\/revisions"}],"predecessor-version":[{"id":1129,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1128\/revisions\/1129"}],"wp:attachment":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1128"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1128"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1128"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}