Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

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) {
        String twitterFormat = "EEE MMM dd HH:mm:ss ZZZZZ yyyy";
        SimpleDateFormat sf = new SimpleDateFormat(twitterFormat, Locale.ENGLISH);
        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();
        }

        // Shorten relative date
        relativeDate = relativeDate.replaceAll(" ", "").replace("seconds", "s").replace("second", "s")
                .replace("minutes", "m").replace("minute", "m").replace("hours", "h").replace("hour", "h")
                .replace("days", "d").replace("day", "d").replace("weeks", "w").replace("week", "w")
                .replace("months", "M").replace("month", "M").replace("years", "y").replace("year", "y")
                .replace("ago", "").replace("in", "").replace("0s", "just now");

        return relativeDate;
    }
}