-1

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);
Priyank Patel
  • 12,244
  • 8
  • 65
  • 85
user3115198
  • 107
  • 2
  • 3
  • 8

4 Answers4

0

Step 1: Create a formatter with a format matching your input.

Step 2: Use parse, not format, to parse it.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

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);
Chirag Ghori
  • 4,231
  • 2
  • 20
  • 35
0

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

Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243
0

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
aNish
  • 1,079
  • 6
  • 11