The following code is giving me the parsed date as "Wed Jan 13 00:00:00 EST 2010" instead of "Wed Jun 13 00:00:00 EST 2010". Any ideas much appreciated.
SimpleDateFormat sf = new SimpleDateFormat("yyyy-mm-dd'T'HH:mm:ss");
String str = "2010-06-13T00:00:00";
Date date = sf.parse(str);
System.out.println(" Date " + date.toString());
Modern answer:
Output:
I am using and recommending
java.time
, the modern Java date and time API. We don’t even need an explicit formatter for parsing. This is because your string is in ISO 8601 format, the international standard that thejava.time
classes parse as their default.java.time
came out in 2014.While in 2010 when this question was asked,
SimpleDateFormat
was what we had for parsing dates and times, that class is now considered long outdated, fortunately, because it was also troublesome.In case your string contained only a date without time of day, use the
LocalDate
class in quite the same manner (this was asked in a duplicate question).Link: Oracle tutorial: Date Time explaining how to use
java.time
.The problem is that you're using 'mm' as month and 'mm' represents minutes. Below is all date formats available, read more doc here.
Example if Date is 06 07 2016
you can use comma, full-stop, slash, or hyphen between these format.
Try:
MM
means month.mm
means minutes. See the documentation forSimpleDateFormat
for more details of the supported date and time patterns.