{"id":503,"date":"2013-10-15T16:29:59","date_gmt":"2013-10-15T08:29:59","guid":{"rendered":"http:\/\/coderbee.net\/?p=503"},"modified":"2013-10-15T16:31:58","modified_gmt":"2013-10-15T08:31:58","slug":"java-8-%e4%b9%8b-java-time-%e5%8c%85","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/java\/20131015\/503","title":{"rendered":"Java 8 \u4e4b java.time \u5305"},"content":{"rendered":"<h3>\u5305\u6982\u8ff0<\/h3>\n<p><a href=\"http:\/\/download.java.net\/jdk8\/docs\/api\/java\/time\/package-summary.html\">java.time \u5305<\/a>\u662f\u5728JDK8\u65b0\u5f15\u5165\u7684\uff0c\u63d0\u4f9b\u4e86\u7528\u4e8e\u65e5\u671f\u3001\u65f6\u95f4\u3001\u5b9e\u4f8b\u548c\u5468\u671f\u7684\u4e3b\u8981API\u3002<\/p>\n<p>java.time\u5305\u5b9a\u4e49\u7684\u7c7b\u8868\u793a\u4e86\u65e5\u671f-\u65f6\u95f4\u6982\u5ff5\u7684\u89c4\u5219\uff0c\u5305\u62ecinstants, durations, dates, times, time-zones and periods\u3002\u8fd9\u4e9b\u90fd\u662f\u57fa\u4e8eISO\u65e5\u5386\u7cfb\u7edf\uff0c\u5b83\u53c8\u662f\u9075\u5faa Gregorian\u89c4\u5219\u7684\u3002<\/p>\n<p><strong>\u6240\u6709\u7c7b\u90fd\u662f\u4e0d\u53ef\u53d8\u7684\u3001\u7ebf\u7a0b\u5b89\u5168\u7684\u3002<\/strong><\/p>\n<p><!--more--><\/p>\n<h3>\u4e00\u4e9b\u7c7b\u7684\u4ecb\u7ecd<\/h3>\n<ul>\n<li><a href=\"http:\/\/download.java.net\/jdk8\/docs\/api\/java\/time\/LocalDateTime.html\">LocalDateTime<\/a>\uff1a\u5b58\u50a8\u4e86\u65e5\u671f\u548c\u65f6\u95f4\uff0c\u5982\uff1a<code>2013-10-15T14:43:14.539<\/code>\u3002<\/p>\n<\/li>\n<li>\n<p><a href=\"http:\/\/download.java.net\/jdk8\/docs\/api\/java\/time\/LocalDate.html\">LocalDate<\/a>\uff1a\u5b58\u50a8\u4e86\u65e5\u671f\uff0c\u5982\uff1a<code>2013-10-15<\/code>\u3002<\/p>\n<\/li>\n<li>\n<p><a href=\"http:\/\/download.java.net\/jdk8\/docs\/api\/java\/time\/LocalTime.html\">LocalTime<\/a>\uff1a\u5b58\u50a8\u4e86\u65f6\u95f4\uff0c\u5982\uff1a<code>14:43:14.539<\/code>\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u4e0a\u9762\u7684\u7c7b\u53ef\u4ee5\u7531\u4e0b\u9762\u7684\u7c7b\u7ec4\u5408\u6765\uff1a<\/p>\n<ul>\n<li><a href=\"http:\/\/download.java.net\/jdk8\/docs\/api\/java\/time\/Year.html\">Year<\/a>\uff1a\u8868\u793a\u5e74\u4efd\u3002<\/p>\n<\/li>\n<li>\n<p><a href=\"http:\/\/download.java.net\/jdk8\/docs\/api\/java\/time\/Month.html\">Month<\/a>\uff1a\u8868\u793a\u6708\u4efd\u3002<\/p>\n<\/li>\n<li>\n<p><a href=\"http:\/\/download.java.net\/jdk8\/docs\/api\/java\/time\/YearMonth.html\">YearMonth<\/a>\uff1a\u8868\u793a\u5e74\u6708\u3002<\/p>\n<\/li>\n<li>\n<p><a href=\"http:\/\/download.java.net\/jdk8\/docs\/api\/java\/time\/MonthDay.html\">MonthDay<\/a>\uff1a\u8868\u793a\u6708\u65e5\u3002<\/p>\n<\/li>\n<li>\n<p><a href=\"http:\/\/download.java.net\/jdk8\/docs\/api\/java\/time\/DayOfWeek.html\">DayOfWeek<\/a>\uff1a\u5b58\u50a8\u661f\u671f\u7684\u4e00\u5929\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u7c7b\u4e4b\u95f4\u8f6c\u6362\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"java\">LocalDateTime localDateTime = LocalDateTime.now();\nSystem.out.println(\"localDateTime :\" + localDateTime);\n\nLocalDate localDate = LocalDate.now();\nSystem.out.println(\"localDate :\" + localDate);\n\nLocalTime localtime = LocalTime.now();\nSystem.out.println(\"localtime :\" + localtime);\n\n\/\/  \u83b7\u53d6\u5f53\u524d\u5e74\u4efd\nYear year = Year.now();\nSystem.out.println(\"year :\" + year);\n\/\/   \u4eceYear\u83b7\u53d6LocalDate\nLocalDate localDate1 = year.atDay(59);\nSystem.out.println(\"localDate1 :\" + localDate1);\n\/\/  \u628aLocalTime\u5173\u8054\u5230\u4e00\u4e2aLocalDate\u5f97\u5230\u4e00\u4e2aLocalDateTime\nLocalDateTime localDateTime1 = localtime.atDate(localDate1);\nSystem.out.println(\"localDateTime1 :\" + localDateTime1);\n\n\/\/  \u7528\u6307\u5b9a\u7684\u5e74\u83b7\u53d6\u4e00\u4e2aYear\nYear year1 = Year.of(2012);\nSystem.out.println(\"year1 :\" + year1);\n\n\/\/  \u4eceYear\u83b7\u53d6YearMonth\nYearMonth yearMonth = year1.atMonth(2);\nSystem.out.println(\"yearMonth :\" + yearMonth);\n\/\/  YearMonth\u6307\u5b9a\u65e5\u5f97\u5230 LocalDate\nLocalDate localDate2 = yearMonth.atDay(29);\nSystem.out.println(\"localDate2 :\" + localDate2);\n\/\/  \u5224\u65ad\u662f\u5426\u662f\u95f0\u5e74\nSystem.out.println(\"isLeapYear :\" + localDate2.isLeapYear());\n\n\/\/\u81ea\u52a8\u5904\u7406\u95f0\u5e74\u76842\u6708\u65e5\u671f\n\/\/\u521b\u5efa\u4e00\u4e2a MonthDay\nMonthDay monthDay = MonthDay.of(2, 29);\nLocalDate leapYear = monthDay.atYear(2012);\nSystem.out.println(\"leapYear :\" + leapYear);\n\n\/\/\u540c\u4e00\u4e2a MonthDay \u5173\u8054\u5230\u53e6\u4e00\u4e2a\u5e74\u4efd\u4e0a\nLocalDate nonLeapYear = monthDay.atYear(2011);\nSystem.out.println(\"nonLeapYear :\" + nonLeapYear);\n\n<\/code><\/pre>\n<p>\u4e0a\u9762\u4ee3\u7801\u7684\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre>\nlocalDateTime :2013-10-15T15:11:57.489\nlocalDate :2013-10-15\nlocaltime :15:11:57.489\nyear :2013\nlocalDate1 :2013-02-28\nlocalDateTime1 :2013-02-28T15:11:57.489\nyear1 :2012\nyearMonth :2012-02\nlocalDate2 :2012-02-29\nisLeapYear :true\nleapYear :2012-02-29\nnonLeapYear :2011-02-28\n<\/pre>\n<h3>\u683c\u5f0f\u5316\u4e0e\u65f6\u95f4\u8ba1\u7b97<\/h3>\n<ul>\n<li><a href=\"http:\/\/download.java.net\/jdk8\/docs\/api\/java\/time\/format\/DateTimeFormatter.html\">DateTimeFormatter<\/a>\uff1a\u5728\u65e5\u671f\u5bf9\u8c61\u4e0e\u5b57\u7b26\u4e32\u4e4b\u95f4\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n<\/li>\n<li>\n<p><a href=\"http:\/\/download.java.net\/jdk8\/docs\/api\/java\/time\/temporal\/ChronoUnit.html\">ChronoUnit<\/a>\uff1a\u8ba1\u7b97\u51fa\u4e24\u4e2a\u65f6\u95f4\u70b9\u4e4b\u95f4\u7684\u65f6\u95f4\u8ddd\u79bb\uff0c\u53ef\u6309\u591a\u79cd\u65f6\u95f4\u5355\u4f4d\u8ba1\u7b97\u3002<\/p>\n<\/li>\n<li>\n<p><a href=\"http:\/\/download.java.net\/jdk8\/docs\/api\/java\/time\/temporal\/TemporalAdjuster.html\">TemporalAdjuster<\/a>\uff1a\u5404\u79cd\u65e5\u671f\u8ba1\u7b97\u529f\u80fd\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u7eed\u524d\u9762\u7684\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"java\">DayOfWeek dayOfWeek = DayOfWeek.of(1);\nSystem.out.println(\"dayOfWeek :\" + dayOfWeek);\n\n\/\/\u8ba1\u7b97\u4e24\u4e2a\u65e5\u671f\u4e4b\u95f4\u7684\u5929\u6570\uff0c\u8fd8\u53ef\u4ee5\u6309\u5176\u4ed6\u65f6\u95f4\u5355\u4f4d\u8ba1\u7b97\u4e24\u4e2a\u65f6\u95f4\u70b9\u4e4b\u95f4\u7684\u95f4\u9694\u3002\nlong between = ChronoUnit.DAYS.between(localDate, leapYear);\nSystem.out.println(\"between :\" + between);\n\n\/\/ \u7ebf\u7a0b\u5b89\u5168\u7684\u683c\u5f0f\u5316\u7c7b\uff0c\u4e0d\u7528\u6bcf\u6b21\u90fdnew\u4e2aSimpleDateFormat\nDateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(\"uuuu MM dd\");\n\n\/\/  \u628a\u65e5\u671f\u65f6\u95f4\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u6807\u8bc6\nSystem.out.println(\"date formatter :\" + dateTimeFormatter.format(nonLeapYear));\n\n\/\/  \u89e3\u6790\u5b57\u7b26\u4e32\u5f62\u5f0f\u7684\u65e5\u671f\u65f6\u95f4\nTemporalAccessor temporalAccessor = dateTimeFormatter.parse(\"2013 01 15\");\nSystem.out.println(\"temporalAccessor :\" + LocalDate.from(temporalAccessor));\n\nInstant instant = Instant.now(); \/\/  \u65f6\u95f4\u6233\nSystem.out.println(\"instant :\" + instant);\n\n\/\/\u8ba1\u7b97\u67d0\u6708\u7684\u7b2c\u4e00\u5929\u7684\u65e5\u671f\nLocalDate with = nonLeapYear.with(TemporalAdjuster.firstDayOfMonth());\nSystem.out.println(\"with :\" + with);\n\n\/\/ \u8ba1\u7b97\u67d0\u6708\u7684\u7b2c\u4e00\u4e2a\u661f\u671f\u4e00\u7684\u65e5\u671f\nTemporalAdjuster temporalAdjuster = TemporalAdjuster.firstInMonth(DayOfWeek.MONDAY);\nLocalDate with1 = localDate.with(temporalAdjuster);\nSystem.out.println(\"with1 :\" + with1);\n\n\/\/ \u8ba1\u7b97localDate\u7684\u4e0b\u4e00\u4e2a\u661f\u671f\u4e00\u7684\u65e5\u671f\nLocalDate with2 = localDate.with(TemporalAdjuster.next(DayOfWeek.MONDAY));\nSystem.out.println(\"with2 :\" + with2);\n<\/code><\/pre>\n<p>\u8f93\u51fa\uff1a<\/p>\n<pre>\ndayOfWeek :MONDAY\nbetween :-594\ndate formatter :2011 02 28\ntemporalAccessor :2013-01-15\ninstant :2013-10-15T07:55:30.964Z\nwith :2011-02-01\nwith1 :2013-10-07\nwith2 :2013-10-21\n<\/pre>\n<h3>java.util.Date\u5230\u65b0\u5e93\u7c7b\u7684\u8f6c\u6362<\/h3>\n<p>\u8f6c\u6362\u53ef\u901a\u8fc7\u4e0b\u9762\u7684\u65b9\u6cd5\u8fdb\u884c\u3002<\/p>\n<pre><code class=\"java\">Date.toInstant()\nDate.from(Instant)\nCalendar.toInstant()\n<\/code><\/pre>\n<h3>\u65b9\u6cd5\u6982\u89c8<\/h3>\n<p>\u8be5\u5305\u7684API\u63d0\u4f9b\u4e86\u5927\u91cf\u76f8\u5173\u7684\u65b9\u6cd5\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u4e00\u822c\u6709\u4e00\u81f4\u7684\u65b9\u6cd5\u524d\u7f00\uff1a<\/p>\n<ul>\n<li>of\uff1a\u9759\u6001\u5de5\u5382\u65b9\u6cd5\u3002<\/li>\n<li>parse\uff1a\u9759\u6001\u5de5\u5382\u65b9\u6cd5\uff0c\u5173\u6ce8\u4e8e\u89e3\u6790\u3002<\/li>\n<li>get\uff1a\u83b7\u53d6\u67d0\u4e9b\u4e1c\u897f\u7684\u503c\u3002<\/li>\n<li>is\uff1a\u68c0\u67e5\u67d0\u4e9b\u4e1c\u897f\u7684\u662f\u5426\u662ftrue\u3002<\/li>\n<li>with\uff1a\u4e0d\u53ef\u53d8\u7684setter\u7b49\u4ef7\u7269\u3002<\/li>\n<li>plus\uff1a\u52a0\u4e00\u4e9b\u91cf\u5230\u67d0\u4e2a\u5bf9\u8c61\u3002<\/li>\n<li>minus\uff1a\u4ece\u67d0\u4e2a\u5bf9\u8c61\u51cf\u53bb\u4e00\u4e9b\u91cf\u3002<\/li>\n<li>to\uff1a\u8f6c\u6362\u5230\u53e6\u4e00\u4e2a\u7c7b\u578b\u3002<\/li>\n<li>at\uff1a\u628a\u8fd9\u4e2a\u5bf9\u8c61\u4e0e\u53e6\u4e00\u4e2a\u5bf9\u8c61\u7ec4\u5408\u8d77\u6765\uff0c\u4f8b\u5982\uff1a<code>date.atTime(time)<\/code>\u3002<\/li>\n<\/ul>\n<p>\u60f3\u8d77\u4ee5\u524d\u505a\u62a5\u8868\u90fd\u662f\u5728\u5b58\u50a8\u8fc7\u7a0b\u91cc\u5904\u7406\u65e5\u671f\u7684\uff0c\u56e0\u4e3aJava\u7684\u65e5\u671f\u64cd\u4f5c\u786e\u5b9e\u592a\u5f31\u4e86\u3002\u6709\u4e86Java 8\uff0c\u5988\u5988\u518d\u4e5f\u4e0d\u7528\u62c5\u5fc3\u6211\u5904\u7406\u65e5\u671f\u4e86\u3002<\/p>\n<hr\/>\n<p>\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u5fae\u4fe1\u516c\u4f17\u53f7: <strong>coderbee\u7b14\u8bb0<\/strong>\uff0c\u53ef\u4ee5\u66f4\u53ca\u65f6\u56de\u590d\u4f60\u7684\u8ba8\u8bba\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"258\" height=\"258\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2019\/01\/coderbee-note.jpg\" class=\"alignnone size-full wp-image-1707\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5305\u6982\u8ff0 java.time \u5305\u662f\u5728JDK8\u65b0\u5f15\u5165\u7684\uff0c\u63d0\u4f9b\u4e86\u7528\u4e8e\u65e5\u671f\u3001\u65f6\u95f4\u3001\u5b9e\u4f8b\u548c &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/java\/20131015\/503\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[91],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/503"}],"collection":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/comments?post=503"}],"version-history":[{"count":7,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/503\/revisions"}],"predecessor-version":[{"id":510,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/503\/revisions\/510"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=503"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=503"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=503"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}