{"id":969,"date":"2019-04-22T18:55:54","date_gmt":"2019-04-22T09:55:54","guid":{"rendered":"https:\/\/chat-messenger.com\/?p=969"},"modified":"2019-10-19T13:22:04","modified_gmt":"2019-10-19T04:22:04","slug":"simpledateformat-parse","status":"publish","type":"post","link":"https:\/\/chat-messenger.com\/en\/blog\/java\/simpledateformat-parse","title":{"rendered":"Convert Java date string to Date type"},"content":{"rendered":"<h2><strong>Java<\/strong> <strong>date string<\/strong>indicates object of desire, like, hate, etc. <strong>java.util.Date<\/strong> Obtained by type<\/h2>\n<p>\nJava to convert a date string (in format yyyy\/MM\/dd ) to a <strong>java.util.Date<\/strong> The following is a sample program that converts to the\n<\/p>\n<h2>sample code<\/h2>\n<pre class=\"sample_src\">\r\n\/**\r\n * Converts date string \"yyyy\/MM\/dd\" to java.util.\r\n Date type * @param str String to be converted\r\n * Date object after conversion.\r\n * @throws ParseException if date string is not \"yyyy\/MM\/dd\".\r\n *\/\/ public static Date toDate(String)\r\npublic static Date toDate(String str) throws ParseException {\r\n    Date date = DateFormat.getDateInstance().parse(str);\r\n    return date; }\r\n}\r\n<\/pre>\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>Execution Result<\/h2>\n<h4>sample program<\/h4>\n<pre class=\"sample_src\">\r\npublic static void main(String[] args) {\r\n    try {\r\n        \/\/ Normal pattern\r\n        Date date = toDate(\"2007\/01\/01\");\r\n        System.out.println(date);\r\n\r\n        \/\/ Pattern with different format\r\n        date = toDate(\"2007-01-01\");\r\n        System.out.println(date);\r\n    } catch (ParseException e) {\r\n        e.printStackTrace(); }\r\n    }\r\n}\r\n<\/pre>\n<h4>output (result)<\/h4>\n<pre class=\"console\">2007\/01\/01='Mon Jan 01 00:00:00 JST 2007'\r\n<\/pre>\n<p>\u3000The yyyy\/MM\/dd date string seems to have been converted correctly, but what about yyyy-MM-dd?<\/p>\n<pre class=\"console\">java.text.ParseException: Unparseable date: \"2007-01-01\"\r\n    at java.text.DateFormat.parse(DateFormat.java:335)\r\n    at Main.toDate(DateUtil.java:627)\r\n    at Main.main(DateUtil.java:639)\r\n<\/pre>\n<p>\u3000ParseException was raised in yyyy-MM-dd.<\/p>\n<p><strong>DateFormat<\/strong> subclass of the <strong>SimpleDateFormat<\/strong> and write the following to convert it,<\/p>\n<pre class=\"sample_src\">SimpleDateFormat format = new SimpleDateFormat(\"yyyy-MM-dd\");\r\nformat.parse(\"2007-01-01\");  \r\n<\/pre>\n<p>I would like to see a more generic program.<br \/>\nNext, we will show you how to convert any date string to the java.util.<br \/>\n<a href=\"https:\/\/chat-messenger.com\/en\/blog\/java\/every-date-string-tocalendar\/\">Convert any date\/time string to Date or Calendar type.<\/a><\/p>","protected":false},"excerpt":{"rendered":"<p>Get Java date string as java.util.Date type Date string in Java (format yyy [\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\/969"}],"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=969"}],"version-history":[{"count":10,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/posts\/969\/revisions"}],"predecessor-version":[{"id":4620,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/posts\/969\/revisions\/4620"}],"wp:attachment":[{"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/media?parent=969"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/categories?post=969"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/tags?post=969"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}