get Relative Time Ago in Twitter format - Android java.util

Android examples for java.util:Date Format

Description

get Relative Time Ago in Twitter format

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 rawJson) {
        String twitterFormat = "EEE MMM dd HH:mm:ss ZZZZZ yyyy";
        SimpleDateFormat sf = new SimpleDateFormat(twitterFormat,
                Locale.ENGLISH);//  w w  w. j  a v  a 2  s.c  o  m
        sf.setLenient(true);

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

        return relativeDate;
    }
}

Related Tutorials