DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("MM/dd/uuuu");
System.out.println(LocalDate.parse("08/16/2011", dateFormatter));
Output:
2011-08-16
I am contributing the modern answer. The answer by Bohemian is correct and was a good answer when it was written 6 years ago. Now the notoriously troublesome SimpleDateFormat
class is long outdated and we have so much better in java.time
, the modern Java date and time API. I warmly recommend you use this instead of the old date-time classes.
When I parse 08/16/2011
using your snippet, I get Sun Jan 16 00:08:00 CET 2011
. Since lowercase mm
is for minutes, I get 00:08:00
(8 minutes past midnight), and since uppercase DD
is for day of year, I get 16 January.
In java.time
too format pattern strings are case sensitive, and we needed to use uppercase MM
for month and lowercase dd for day of month.
Yes, java.time
works nicely on Java 6 and later and on both older and newer Android devices.
org.threeten.bp
with subpackages.java.time
.java.time
was first described.java.time
to Java 6 and 7 (ThreeTen for JSR-310).