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:
org.ocpsoft.prettytime
prettytime
3.2.7.Final
or an sbt dependency if you use an activator project like play framework.
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.
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.
Information about Data protection