Here you can find the source of formatTimeAgo(int seconds)
Parameter | Description |
---|---|
seconds | a parameter |
public static String formatTimeAgo(int seconds)
//package com.java2s; //License from project: Open Source License public class Main { /**/*from w w w . j ava2 s. c o m*/ * Convert seconds into a human readable format. * * @param seconds * @return seconds. */ public static String formatTimeAgo(int seconds) { if (seconds == 0) return "0 seconds"; // @note: 0 seconds is a special case. String date = ""; String[] unitNames = { "week", "day", "hour", "minute", "second" }; int[] unitValues = { 604800, 86400, 3600, 60, 1 }; // Loop through all of the units. for (int i = 0; i < unitNames.length; i++) { int quot = seconds / unitValues[i]; if (quot > 0) { date += quot + " " + unitNames[i] + (Math.abs(quot) > 1 ? "s" : "") + ", "; seconds -= (quot * unitValues[i]); } } // Return the date, substring -2 to remove the trailing ", ". return date.substring(0, date.length() - 2); } /** * Convert milliseconds into a human readable format. * * @param milliseconds * @return String */ public static String formatTimeAgo(Long milliseconds) { return formatTimeAgo((int) (milliseconds / 1000)); } }