Covert UTC time format to local time

sukarno picture sukarno · Mar 19, 2013 · Viewed 7.8k times · Source

I have a UTC time in this format 2013-03-04T14:37:15 How can I convert this into local time and then to one minute ago format.

I tried this first to convert the time to local format:

private String getDate(String date) {
        Date fDate = null;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
        try {
            fDate = simpleDateFormat.parse(date);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
return fDate.toString();
    }

But the above functions throws an exception saying unparseable date..

Answer

RainMaker picture RainMaker · Mar 19, 2013

The parse exception you get is because of the extra T in your date(2013-03-04T14:37:15).

You can either remove that extra 'T' and parse the date with your current date format,

Or if you need to parse it with the T, change your format to parse the literal T as well,

new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");