How can i convert 2004-07-04T11:45:52
to date July 07, 2014
in java.
My work:
SimpleDateFormat formatter = new SimpleDateFormat("MMMMMMM dd, yyyy");
String date = "2004-07-04T11:45:52";
String mdate = formatter.format(date);
How can i convert 2004-07-04T11:45:52
to date July 07, 2014
in java.
My work:
SimpleDateFormat formatter = new SimpleDateFormat("MMMMMMM dd, yyyy");
String date = "2004-07-04T11:45:52";
String mdate = formatter.format(date);
Step 1: Create a formatter with a format matching your input.
Step 2: Use parse
, not format
, to parse it.
Try something like this
SimpleDateFormat form = new SimpleDateFormat("yyyy-dd-MM'T'HH:mm:ss");
String inputdate = "2004-07-04T11:45:52";
Date date = null;
try {
date = form.parse(inputdate);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat postFormater = new SimpleDateFormat("MMM dd, yyyy");
String resultdate = postFormater.format(date);
In your input you have given year as 2004 but you are expecting output year 2014 which you will not get. But you will get 2004 as year not 2014. same problem for month also The following sample will give you the result as Jul 04, 2004
String newstr = "2004-07-04T11:45:52";
SimpleDateFormat inFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
SimpleDateFormat outFormat = new SimpleDateFormat("MMM dd, yyyy");
Calendar c = Calendar.getInstance();
c.setTime(inFormat.parse(newstr));
System.out.println(outFormat.format(c.getTime()));
output Jul 04, 2004
Joda time would be good choice when dealing with Dates (although there's no date manipulation in the question here). But Joda time does help simplify and you could write something like the following:
public static void dateTest()
{
String date = "2004-07-04T11:45:52";
DateTime dateTime = new DateTime(date);
System.out.println(dateTime);
System.out.println(dateTime.toString("MMMM dd, yyyy"));
}
This would give:
2004-07-04T11:45:52.000+04:00
July 04, 2004