Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fancy time-formatter displaying "5 min ago", "yesterday" etc [duplicate]

What is the best way to format time like modern web-pages (including StackOverflow ;-) do, i.e. using simple relative descriptions like:

  • 5 seconds ago
  • 23 minutes ago
  • yesterday
  • 2 days ago
  • ...

I am looking for a library that can handle this in addition to multiple locales. I can supply the phrases ( "X seconds" in English, "X Sekunden" in German etc.).

like image 896
Queequeg Avatar asked Mar 26 '13 08:03

Queequeg


1 Answers

I think this is what you are looking for :

How to calculate "time ago" in Java?

The lib: PrettyTime

For German:

PrettyTime p = new PrettyTime(new Locale("de"));
System.out.println(p.format(new Date()));

For English:

PrettyTime p = new PrettyTime();
System.out.println(p.format(new Date()));

Apparently available in 25 languages.

like image 152
Michaël Avatar answered Sep 20 '22 09:09

Michaël