get Relative Time Ago - Android java.util

Android examples for java.util:Time

Description

get Relative Time Ago

Demo Code


//package com.java2s;
import android.text.format.DateUtils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;

public class Main {
    public static String getRelativeTimeAgo(String rawJsonDate) {
        // 2015-12-19T03:00:18.752-08:00
        String productHuntFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
        SimpleDateFormat sf = new SimpleDateFormat(productHuntFormat,
                Locale.ENGLISH);// w  ww.j  a  v  a  2  s. com
        sf.setLenient(true);

        String relativeDate = "";
        try {
            long dateMillis = sf.parse(rawJsonDate).getTime();
            relativeDate = DateUtils.getRelativeTimeSpanString(dateMillis,
                    System.currentTimeMillis(), DateUtils.SECOND_IN_MILLIS)
                    .toString();
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return relativeDate;
    }
}

Related Tutorials