{"id":4718,"date":"2019-11-02T21:58:50","date_gmt":"2019-11-02T12:58:50","guid":{"rendered":"https:\/\/chat-messenger.com\/?p=4718"},"modified":"2024-01-14T02:34:30","modified_gmt":"2024-01-13T17:34:30","slug":"string-valueof-integer-parseint","status":"publish","type":"post","link":"https:\/\/chat-messenger.com\/zh\/\u535a\u5ba2\/java\/\u5b57\u7b26\u4e32","title":{"rendered":"Java \u6570\u503c \u21d4 \u5b57\u7b26\u4e32 \u21d4 \u65e5\u671f\u8f6c\u6362"},"content":{"rendered":"<h2>\u6570\u5b57 \u21d2 \u5b57\u7b26\u4e32\u8f6c\u6362<\/h2>\n\n\n\n<p>\u6570\u5b57\u5230\u5b57\u7b26\u4e32\u7684\u8f6c\u6362\u662f<strong>String.valueof()<\/strong> \u4f7f\u7528\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">String str = String.valueOf(num);.<\/pre>\n\n\n\n<p>\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u5199\u6cd5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">String str = Integer.toString(num); String str = &quot;&quot; + num;<\/pre>\n\n\n\n<p>\u6700\u540e\u7684\"\" + num \u5229\u7528\u4e86 Java \u7684\u4e00\u4e2a\u7279\u6027\uff0c\u5373\u5728\u4e0e\u5b57\u7b26\u4e32\u8fde\u63a5\u65f6\u5c06\u6570\u5b57\u89c6\u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n\n\n\n<p>\u6700\u540e\u4e00\u79cd\u65b9\u6cd5\u63cf\u8ff0\u8d77\u6765\u6700\u7b80\u5355\uff0c\u4f46\u5982\u679c\u4e0d\u4e86\u89e3 Java \u7279\u6027\u7684\u4eba\u770b\u5230\u5b83\uff0c\u5c31\u9700\u8981\u82b1\u65f6\u95f4\u6765\u89e3\u8bfb\u5176\u610f\u56fe\u3002<\/p>\n\n\n\n<h2>\u5b57\u7b26\u4e32 \u21d2 \u6570\u503c\u8f6c\u6362<\/h2>\n\n\n\n<p>\u5b57\u7b26\u4e32\u5230\u6570\u5b57\u7684\u8f6c\u6362\u6709<strong>Integer.parseInt()\u3002<\/strong> \u4f7f\u7528\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">int num = Integer.parseInt(str);.<\/pre>\n\n\n\n<p>\u8bf7\u6ce8\u610f\uff0c\u5982\u679c\u7531\u4e8e\u67d0\u79cd\u539f\u56e0\u65e0\u6cd5\u5b8c\u6210\u8f6c\u6362\uff0c\u4f8b\u5982\u6570\u5b57\u5305\u542b\u5b57\u7b26\u6216\u6570\u5b57\u4e0d\u9002\u5408\u67d0\u79cd\u7c7b\u578b\uff0c\u5219\u4f1a\u5f15\u53d1 NumberFormatException \u5f02\u5e38\u3002<\/p>\n\n\n\n<h2>\u65e5\u671f \u21d2 \u5b57\u7b26\u4e32\u8f6c\u6362<\/h2>\n\n\n\n<p>\u5c06\u65e5\u671f\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u6bd4\u6570\u5b57\u21d2\u5b57\u7b26\u4e32\u7a0d\u5fae\u590d\u6742\u4e00\u4e9b\u3002<\/p>\n\n\n\n<p>\u9996\u5148 <strong>\u7b80\u5355\u65e5\u671f\u683c\u5f0f<\/strong> \u521b\u5efa\u4e00\u4e2a\u521b\u5efa\u65f6\uff0c\u9700\u8981\u6307\u5b9a\u65e5\u671f\u683c\u5f0f\u3002\u8fd9\u5c06\u662f\u65e5\u671f\u8f6c\u6362\u6210\u5b57\u7b26\u4e32\u65f6\u7684\u683c\u5f0f\u3002<\/p>\n\n\n\n<p>\u6700\u540e <strong>SimpleDateFormat.format()<\/strong> \u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n\n\n\n<h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n\n\n\n<div class=\"hcb_wrap\" data-no-translation=\"\"><pre class=\"prism line-numbers lang-java\" data-lang=\"Java\"><code>public static void main(String[] args) {\n    Date date = new Date();\n    SimpleDateFormat sdf = new SimpleDateFormat(&quot;yyyy\/MM\/dd hh:mm:ss&quot;);\n    String str = sdf.format(date);\n    \n    System.out.println(&quot;\u65e5\u4ed8\u578b = &quot; + date);\n    System.out.println(&quot;\u6587\u5b57\u5217 = &quot; + str);\n}<\/code><\/pre><\/div>\n\n\n\n<h4>\u8f93\u51fa\u7ed3\u679c\uff1a<\/h4>\n\n\n\n<div class=\"hcb_wrap\" data-no-translation=\"\"><pre class=\"prism line-numbers lang-bash\" data-lang=\"Bash\"><code>\u65e5\u4ed8\u578b = Sat Nov 02 12:11:55 UTC 2019\n\u6587\u5b57\u5217 = 2019\/11\/02 12:11:55<\/code><\/pre><\/div>\n\n\n\n<h2>\u5b57\u7b26\u4e32 \u21d2 \u65e5\u671f\u8f6c\u6362<\/h2>\n\n\n\n<p>\u7528\u4e8e\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u65e5\u671f\u3001<strong>SimpleDateFormat.parse<\/strong> \u4f7f\u7528\u8be5\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p>\u65e5\u671f \u21d2 \u4e0e\u5b57\u7b26\u4e32\u4e00\u6837\uff0c\u521b\u5efa\u4e00\u4e2a SimpleDateFormat\u3002\u521b\u5efa\u65f6\u4f1a\u6307\u5b9a\u65e5\u671f\u683c\u5f0f\uff0c\u8be5\u683c\u5f0f\u4e0e\u8981\u8f6c\u6362\u7684\u5b57\u7b26\u4e32\u7684\u65e5\u671f\u683c\u5f0f\u4e00\u81f4\u3002<\/p>\n\n\n\n<p>\u6700\u540e <strong>SimpleDateFormat.parse()<\/strong> \u81f3\u4eca\uff08\u65e5\u671f\uff09\u3002<\/p>\n\n\n\n<h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n\n\n\n<div class=\"hcb_wrap\" data-no-translation=\"\"><pre class=\"prism line-numbers lang-java\" data-lang=\"Java\"><code>public static void main(String[] args) {\n    try {\n        String strDate = &quot;2019\/11\/01 12:34:56&quot;;\n     \n        SimpleDateFormat sdf = new SimpleDateFormat(&quot;yyyy\/MM\/dd hh:mm:ss&quot;);\n        Date date = sdf.parse(strDate);\n        \n        System.out.println(&quot;\u6587\u5b57\u5217 = &quot; + strDate);\n        System.out.println(&quot;\u65e5\u4ed8\u578b = &quot; + date);\n        \n    } catch (ParseException e) {\n        \/\/\u4f8b\u5916\u51e6\u7406\n    }\n}<\/code><\/pre><\/div>\n\n\n\n<h4>\u8f93\u51fa\u7ed3\u679c\uff1a<\/h4>\n\n\n\n<div class=\"hcb_wrap\" data-no-translation=\"\"><pre class=\"prism line-numbers lang-bash\" data-lang=\"Bash\"><code>\u6587\u5b57\u5217 = 2019\/11\/01 12:34:56\n\u65e5\u4ed8\u578b = Fri Nov 01 00:34:56 UTC 2019<\/code><\/pre><\/div>\n\n\n\n<p>\u8bf7\u6ce8\u610f\uff0c\u89e3\u6790\u65b9\u6cd5\u4f1a\u629b\u51fa ParseException \u5f02\u5e38\uff0c\u5fc5\u987b\u7528 try-catch \u8bed\u53e5\u62ec\u8d77\u6765\uff0c\u5426\u5219\u4f1a\u88ab\u91cd\u65b0\u629b\u51fa\u3002<\/p>\n\n\n\n<h2>\u6570\u5b57 \u21d4 \u65e5\u671f\u8f6c\u6362<\/h2>\n\n\n\n<p>\u4e0d\u5e78\u7684\u662f\uff0c\u6ca1\u6709\u8bbe\u8ba1\u7528\u4e8e\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u65e5\u671f\u6216\u5c06\u65e5\u671f\u8f6c\u6362\u4e3a\u6570\u5b57\u7684\u65b9\u6cd5\u3002\u4e24\u8005\u90fd\u9700\u8981\u7ecf\u8fc7\u4e00\u6b21\u5b57\u7b26\u4e32\u8f6c\u6362\u3002<\/p>","protected":false},"excerpt":{"rendered":"<p>\u4ece\u5b57\u7b26\u4e32\u8f6c\u6362\u6570\u5b57 \u4f7f\u7528 String.valueof() \u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002\u7ec6\u7ef3 [\u2026]<\/p>","protected":false},"author":1,"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\/4718"}],"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\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/chat-messenger.com\/zh\/wp-json\/wp\/v2\/comments?post=4718"}],"version-history":[{"count":7,"href":"https:\/\/chat-messenger.com\/zh\/wp-json\/wp\/v2\/posts\/4718\/revisions"}],"predecessor-version":[{"id":8895,"href":"https:\/\/chat-messenger.com\/zh\/wp-json\/wp\/v2\/posts\/4718\/revisions\/8895"}],"wp:attachment":[{"href":"https:\/\/chat-messenger.com\/zh\/wp-json\/wp\/v2\/media?parent=4718"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chat-messenger.com\/zh\/wp-json\/wp\/v2\/categories?post=4718"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chat-messenger.com\/zh\/wp-json\/wp\/v2\/tags?post=4718"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}