-1

I have a function that needs to return a Date, I have a Date String of

    '2016-02-09T12:22:00.000+00:00'

and want to convert it to

    '2016-02-09 12:22:00'

but my return result is a string, in my case i want this to be a date. Here is my function so far i have tried is:

    private Date parse(String s){ // function needs to return date as yyyy-mm-dd HH:mm:ss
       SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
       SimpleDateFormat output = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
       Date d = null;
       try {
           d = sdf.parse(s);
       } catch (ParseException e) {
           e.printStackTrace();
       }
       String formattedTime = output.format(d);
       System.out.println("Formated Date is = "+ formattedTime); // return date in yyyy-mm-dd HH:mm:ss (but a string)
       return date; (Here i want the expected date to be as yyyy-mm-dd HH:mm:ss, I am getting as 'Tue Feb 09 12:22:00 GMT+05:30 2016')
    }

Please help me its been a while trying this and am failing every time.

Thanks.

Waseem Akram
  • 215
  • 2
  • 14

1 Answers1

4

Date has no any format, it's just... date. yyyy-mm-dd HH:mm:ss is string representation according to some format (your code has this), 'Tue Feb 09 12:22:00 GMT+05:30 2016' is result of default toString().

Just parse string into Date object and return it, then call SimpleDateFormat.format(date) wherever it needs to be showed / logged / whatever.

Mikhail Kuchma
  • 583
  • 2
  • 9