Pergunta

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..

Foi útil?

Solução

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");

Outras dicas

Check DateUtils.getRelativeTimeSpanString() (one methods with the same name from DateUtils). It will be something like :

Date fDate = null;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
fDate = simpleDateFormat.parse(date);
return DateUtils.getRelativeTimeSpanString(context, System.currentTimeMillis()-fDate.getTime())'
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top