Prettytime is a nice java library to format a java Date()s in a nice humanized ago format, like:
- moments ago
- 2 minutes ago
- 13 hours ago
- 7 months ago
- 2 years ago
Prettytime is localized in over 30 languages.
It’s super simple to use
Add the dependency to your maven pom:
1 2 3 4 5 |
<dependency> <groupid>org.ocpsoft.prettytime</groupid> <artifactid>prettytime</artifactid> <version>3.2.7.Final</version> </dependency> |
or an sbt dependency if you use an activator project like play framework.
1 |
libraryDependencies += "org.ocpsoft.prettytime" % "prettytime" % "3.2.7.Final" |
Then add the following static method to a helper class and call it from your json processor or your template engine.
1 2 3 4 |
public static String agoHumanized(final Date date, Locale locale){ PrettyTime p = new PrettyTime(locale); return p.format(date); } |
Thats it!
You can find the code on github and the documentation on the ocpsoft page. Happy coding.