Get string for time Elapsed To String Brief - Java java.util

Java examples for java.util:Time Format

Description

Get string for time Elapsed To String Brief

Demo Code

//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        long time = 12312312342;
        System.out.println(timeElapsedToStringBrief(time));
    }//from   w  ww. j  a v  a2  s . c  o  m

    public static final long ONE_YEAR_IN_MILISECONDS = 31530000000L;
    public static final long ONE_MONTH_IN_MILISECONDS = 2628000000L;
    public static final long ONE_WEEK_IN_MILISECONDS = 604800000L;
    public static final long ONE_DAY_IN_MILISECONDS = 86400000L;
    public static final long ONE_HOUR_IN_MILISECONDS = 3600000L;
    public static final long ONE_MINUTE_IN_MILISECONDS = 60000L;
    public static final long ONE_SECOND_IN_MILISECONDS = 1000L;

    /**
     *
     * @param time
     * @return
     */
    public static String timeElapsedToStringBrief(long time) {
        String returnTime = "";
        long temp;

        if (time / ONE_YEAR_IN_MILISECONDS > 0) {
            temp = time / ONE_YEAR_IN_MILISECONDS;
            returnTime += temp + "y";

        } else if (time / ONE_MONTH_IN_MILISECONDS > 0) {
            temp = time / ONE_MONTH_IN_MILISECONDS;
            returnTime += temp + "m";

        } else if (time / ONE_WEEK_IN_MILISECONDS > 0) {
            temp = time / ONE_WEEK_IN_MILISECONDS;
            returnTime += temp + "w";

        } else if (time / ONE_DAY_IN_MILISECONDS > 0) {
            temp = time / ONE_DAY_IN_MILISECONDS;
            returnTime += temp + "d";

        } else if (time / ONE_HOUR_IN_MILISECONDS > 0) {
            temp = time / ONE_HOUR_IN_MILISECONDS;
            returnTime += temp + "h";

        } else if (time / ONE_MINUTE_IN_MILISECONDS > 0) {
            temp = time / ONE_MINUTE_IN_MILISECONDS;
            returnTime += temp + "min";

        } else if (time / ONE_SECOND_IN_MILISECONDS > 0) {
            temp = time / ONE_SECOND_IN_MILISECONDS;
            returnTime += temp + "s";
        } else {
            returnTime = "Just now";
        }
        return returnTime;
    }
}

Related Tutorials