-1

I just want to convert a date from the database to 12 hours format which is originally in 24 hours format which is something like this 2015-06-11T28:28:57.000Z. In here this time format (28:25:57) seems to be the next day (ie. 2015-06-12T04:28:57).

I have tried with the following:

String date = "2015-06-11T28:28:57.000Z";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss a");
Date date = formatter.parse(date);

but I got an error like unparsable date.

Sean Bright
  • 118,630
  • 17
  • 138
  • 146
Nandha King
  • 29
  • 1
  • 10

1 Answers1

0

Your date formatter string was incorrect, Try this:

    String dateStr = "2015-06-11T28:28:57.000Z";
    SimpleDateFormat sdfInput = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
    Date date = sdfInput.parse(dateStr);
    System.out.println("Date:"+date ); //Fri Jun 12 04:28:57 IST 2015

    // to get 12 hour date formate date-string from provided date
    SimpleDateFormat sdfOutput = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS ");
    String dateStr2 = sdfOutput.format(date);
    System.out.println("dateStr2:" + dateStr2); //2015-06-12 04:28:57.000 
Mahendra
  • 1,436
  • 9
  • 15