{"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\/en\/blog\/java\/string-valueof-integer-parseint","title":{"rendered":"Java Numeric\u21d4String\u21d4Date Conversion"},"content":{"rendered":"<h2>Numeric \u21d2 String conversion<\/h2>\n\n\n\n<p>Numeric to string conversion is,<strong>String.valueof()<\/strong> Use the<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">String str = String.valueOf(num);<\/pre>\n\n\n\n<p>There are also other ways to write this.<\/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>The final \"\" + num takes advantage of a Java feature that treats numbers as strings when concatenated with strings.<\/p>\n\n\n\n<p>The last approach is the simplest to describe, but if someone who does not have a thorough understanding of Java's characteristics sees it, it will take time to decipher the intent.<\/p>\n\n\n\n<h2>String \u21d2 Numeric conversion<\/h2>\n\n\n\n<p>String to numeric conversion is,<strong>Integer.parseInt()<\/strong> Use the<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">int num = Integer.parseInt(str);<\/pre>\n\n\n\n<p>Note that a NumberFormatException will be thrown if the conversion fails for some reason, such as the presence of characters or a number that does not fit into the type.<\/p>\n\n\n\n<h2>Date \u21d2 String conversion<\/h2>\n\n\n\n<p>Converting a date to a string is a bit more complicated than number to string.<\/p>\n\n\n\n<p>first (of all) <strong>SimpleDateFormat<\/strong> Create an instance of Specify the date format when you create it. This will be the format when made into a string.<\/p>\n\n\n\n<p>Finally. <strong>SimpleDateFormat.format()<\/strong> to a string.<\/p>\n\n\n\n<h4>sample code<\/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>Output Results:<\/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>String \u21d2 Date conversion<\/h2>\n\n\n\n<p>For string to date conversion,<strong>SimpleDateFormat.parse<\/strong> method.<\/p>\n\n\n\n<p>Create a SimpleDateFormat as in the date \u21d2 string case. When created, the date format is specified, which matches the date format of the string to be converted.<\/p>\n\n\n\n<p>Finally. <strong>SimpleDateFormat.parse()<\/strong> to convert to a date (Date).<\/p>\n\n\n\n<h4>sample code<\/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>Output Results:<\/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>Note that the parse method throws a ParseException, which must be enclosed in a try-catch statement or re-thrown.<\/p>\n\n\n\n<h2>Numeric \u21d4 Date Conversion<\/h2>\n\n\n\n<p>Unfortunately, there are no methods designed to convert numbers to dates or dates to numbers. Both need to go through conversion to string once.<\/p>","protected":false},"excerpt":{"rendered":"<p>Converting numbers from strings Use String.valueof() to convert numbers to strings. String [\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\/en\/wp-json\/wp\/v2\/posts\/4718"}],"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\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/comments?post=4718"}],"version-history":[{"count":7,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/posts\/4718\/revisions"}],"predecessor-version":[{"id":8895,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/posts\/4718\/revisions\/8895"}],"wp:attachment":[{"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/media?parent=4718"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/categories?post=4718"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/tags?post=4718"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}