<p>I have a <code>String</code> representation of a date that I need to create a <code>Date</code> or <code>Calendar</code> object from. I've looked through <code>Date</code> and <code>Calendar</code> APIs but haven't found anything that can do this other than creating my own ugly parse method. I know there must be a way, does anyone know of a solution?</p>

[{'answer_id': 43806, 'author': 'Apocalisp', 'author_id': 3434, 'author_profile': 'https://Stackoverflow.com/users/3434', 'pm_score': 0, 'selected': False, 'text': '<p>Try this:</p>\n\n<pre><code>DateFormat.parse(String)\n</code></pre>\n'}, {'answer_id': 43807, 'author': 'Matt Sheppard', 'author_id': 797, 'author_profile': 'https://Stackoverflow.com/users/797', 'pm_score': 8, 'selected': True, 'text': '<p>In brief:</p>\n\n<pre><code>DateFormat formatter = new SimpleDateFormat("MM/dd/yy");\ntry {\n  Date date = formatter.parse("01/29/02");\n} catch (ParseException e) {\n  e.printStackTrace();\n}\n</code></pre>\n\n<p>See <a href="http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html" rel="noreferrer"><code>SimpleDateFormat</code> javadoc</a> for more.</p>\n\n<p>And to turn it into a <code>Calendar</code>, do:</p>\n\n<pre><code>Calendar calendar = Calendar.getInstance();\ncalendar.setTime(date);\n</code></pre>\n'}, {'answer_id': 43812, 'author': 'Alexander Stolz', 'author_id': 2450, 'author_profile': 'https://Stackoverflow.com/users/2450', 'pm_score': 0, 'selected': False, 'text': '<p>The <code>DateFormat</code> class has a <code>parse</code> method.</p>\n<p>See <a href="https://docs.oracle.com/javase/1.5.0/docs/api/java/text/DateFormat.html" rel="nofollow noreferrer">DateFormat</a> for more information.</p>\n'}, {'answer_id': 44012, 'author': 'serg10', 'author_id': 1853, 'author_profile': 'https://Stackoverflow.com/users/1853', 'pm_score': 3, 'selected': False, 'text': '<p>The <strong>highly regarded <a href="http://joda-time.sourceforge.net/index.html" rel="noreferrer">Joda Time library</a></strong> is also worth a look.  This is basis for the new <a href="http://jcp.org/en/jsr/detail?id=310" rel="noreferrer">date and time api</a> that is pencilled in for Java 7.  The design is neat, intuitive, <a href="http://joda-time.sourceforge.net/userguide.html" rel="noreferrer">well documented</a> and avoids a lot of the clumsiness of the original <code>java.util.Date</code> / <code>java.util.Calendar</code> classes.  </p>\n\n<p>Joda\'s <a href="http://joda-time.sourceforge.net/api-release/org/joda/time/format/DateTimeFormatter.html" rel="noreferrer"><code>DateFormatter</code></a> can parse a String to a Joda <code>DateTime</code>.</p>\n'}, {'answer_id': 33405474, 'author': 'Basil Bourque', 'author_id': 642706, 'author_profile': 'https://Stackoverflow.com/users/642706', 'pm_score': 4, 'selected': False, 'text': '<h1>tl;dr</h1>\n\n<pre><code>LocalDate.parse( "2015-01-02" )\n</code></pre>\n\n<h1>java.time</h1>\n\n<p>Java 8 and later has a new <a href="http://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html" rel="noreferrer">java.time</a> framework that makes these other answers outmoded. This framework is inspired by <a href="http://www.joda.org/joda-time/" rel="noreferrer">Joda-Time</a>, defined by <a href="http://jcp.org/en/jsr/detail?id=310" rel="noreferrer">JSR 310</a>, and extended by the <a href="http://www.threeten.org/threeten-extra/" rel="noreferrer">ThreeTen-Extra</a> project. See the <a href="http://docs.oracle.com/javase/tutorial/java/TOC.html" rel="noreferrer">Tutorial</a>.</p>\n\n<p>The old bundled classes, java.util.Date/.Calendar, are notoriously troublesome and confusing. Avoid them.</p>\n\n<h1><code>LocalDate</code></h1>\n\n<p>Like Joda-Time, java.time has a class <a href="http://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html" rel="noreferrer"><code>LocalDate</code></a> to represent a date-only value without time-of-day and without time zone. </p>\n\n<h1>ISO 8601</h1>\n\n<p>If your input string is in the standard <a href="https://en.wikipedia.org/wiki/ISO_8601" rel="noreferrer">ISO 8601</a> format of <code>yyyy-MM-dd</code>, you can ask that class to directly parse the string with no need to specify a formatter. </p>\n\n<p>The ISO 8601 formats are used by default in java.time, for both parsing and generating string representations of date-time values.</p>\n\n<pre><code>LocalDate localDate = LocalDate.parse( "2015-01-02" );\n</code></pre>\n\n<h1>Formatter</h1>\n\n<p>If you have a different format, specify a formatter from the <a href="http://docs.oracle.com/javase/8/docs/api/java/time/format/package-summary.html" rel="noreferrer">java.time.format</a> package. You can either specify your own formatting pattern or let java.time automatically localize as appropriate to a <code>Locale</code> specifying a human language for translation and cultural norms for deciding issues such as period versus comma.</p>\n\n<h2>Formatting pattern</h2>\n\n<p>Read the <a href="http://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html" rel="noreferrer"><code>DateTimeFormatter</code></a> class doc for details on the codes used in the format pattern. They vary a bit from the old outmoded <code>java.text.SimpleDateFormat</code> class patterns.</p>\n\n<p>Note how the second argument to the <code>parse</code> method is a <a href="http://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html" rel="noreferrer">method reference</a>, syntax added to Java 8 and later.</p>\n\n<pre><code>String input = "January 2, 2015";\nDateTimeFormatter formatter = DateTimeFormatter.ofPattern ( "MMMM d, yyyy" , Locale.US );\nLocalDate localDate = LocalDate.parse ( input , formatter );\n</code></pre>\n\n<p>Dump to console.</p>\n\n<pre><code>System.out.println ( "localDate: " + localDate );\n</code></pre>\n\n<blockquote>\n  <p>localDate: 2015-01-02</p>\n</blockquote>\n\n<h2>Localize automatically</h2>\n\n<p>Or rather than specify a formatting pattern, let java.time localize for you. Call <code>DateTimeFormatter.ofLocalizedDate</code>, and be sure to specify the desired/expected <code>Locale</code> rather than rely on the JVM’s current default which can change at any moment during runtime(!).</p>\n\n<pre><code>String input = "January 2, 2015";\nDateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDate ( FormatStyle.LONG );\nformatter = formatter.withLocale ( Locale.US );\nLocalDate localDate = LocalDate.parse ( input , formatter );\n</code></pre>\n\n<p>Dump to console. </p>\n\n<pre><code>System.out.println ( "input: " + input + " | localDate: " + localDate );\n</code></pre>\n\n<blockquote>\n  <p>input: January 2, 2015 | localDate: 2015-01-02</p>\n</blockquote>\n'}]
['https://Stackoverflow.com/questions/43802', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2628/']