Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Yup awakened the dead I have, but here's my improved implementation based on @mtim posted code, as this thread comes almost on top of the searches so I am messing with the sleepy hollow,</p> <pre><code> public static String getFriendlyTime(Date dateTime) { StringBuffer sb = new StringBuffer(); Date current = Calendar.getInstance().getTime(); long diffInSeconds = (current.getTime() - dateTime.getTime()) / 1000; /*long diff[] = new long[]{0, 0, 0, 0}; /* sec * diff[3] = (diffInSeconds &gt;= 60 ? diffInSeconds % 60 : diffInSeconds); /* min * diff[2] = (diffInSeconds = (diffInSeconds / 60)) &gt;= 60 ? diffInSeconds % 60 : diffInSeconds; /* hours * diff[1] = (diffInSeconds = (diffInSeconds / 60)) &gt;= 24 ? diffInSeconds % 24 : diffInSeconds; /* days * diff[0] = (diffInSeconds = (diffInSeconds / 24)); */ long sec = (diffInSeconds &gt;= 60 ? diffInSeconds % 60 : diffInSeconds); long min = (diffInSeconds = (diffInSeconds / 60)) &gt;= 60 ? diffInSeconds % 60 : diffInSeconds; long hrs = (diffInSeconds = (diffInSeconds / 60)) &gt;= 24 ? diffInSeconds % 24 : diffInSeconds; long days = (diffInSeconds = (diffInSeconds / 24)) &gt;= 30 ? diffInSeconds % 30 : diffInSeconds; long months = (diffInSeconds = (diffInSeconds / 30)) &gt;= 12 ? diffInSeconds % 12 : diffInSeconds; long years = (diffInSeconds = (diffInSeconds / 12)); if (years &gt; 0) { if (years == 1) { sb.append("a year"); } else { sb.append(years + " years"); } if (years &lt;= 6 &amp;&amp; months &gt; 0) { if (months == 1) { sb.append(" and a month"); } else { sb.append(" and " + months + " months"); } } } else if (months &gt; 0) { if (months == 1) { sb.append("a month"); } else { sb.append(months + " months"); } if (months &lt;= 6 &amp;&amp; days &gt; 0) { if (days == 1) { sb.append(" and a day"); } else { sb.append(" and " + days + " days"); } } } else if (days &gt; 0) { if (days == 1) { sb.append("a day"); } else { sb.append(days + " days"); } if (days &lt;= 3 &amp;&amp; hrs &gt; 0) { if (hrs == 1) { sb.append(" and an hour"); } else { sb.append(" and " + hrs + " hours"); } } } else if (hrs &gt; 0) { if (hrs == 1) { sb.append("an hour"); } else { sb.append(hrs + " hours"); } if (min &gt; 1) { sb.append(" and " + min + " minutes"); } } else if (min &gt; 0) { if (min == 1) { sb.append("a minute"); } else { sb.append(min + " minutes"); } if (sec &gt; 1) { sb.append(" and " + sec + " seconds"); } } else { if (sec &lt;= 1) { sb.append("about a second"); } else { sb.append("about " + sec + " seconds"); } } sb.append(" ago"); /*String result = new String(String.format( "%d day%s, %d hour%s, %d minute%s, %d second%s ago", diff[0], diff[0] &gt; 1 ? "s" : "", diff[1], diff[1] &gt; 1 ? "s" : "", diff[2], diff[2] &gt; 1 ? "s" : "", diff[3], diff[3] &gt; 1 ? "s" : ""));*/ return sb.toString(); } </code></pre> <p>It obviously can be improved. basically it tries to get the time span more friendly, there are a few limitation though i.e. it would behave strangely if the time passed (parameter) is in future, and its limited up to the days, hours and seconds only (months and years not handled, so that someone else can ;-).</p> <p>sample outputs are:</p> <ul> <li>about a second ago </li> <li>8 minutes and 34 seconds ago</li> <li>an hour and 4 minutes ago</li> <li>a day ago</li> <li>29 days ago</li> <li>a year and 3 months ago</li> </ul> <p>, cheers :D</p> <p>EDIT: now supports months and years :P</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload