November 14, 2015

Format date and time in java with prettytime

0  comments

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.

Tags

agohumanized, date format, Java, maven, prettytime, sbt


You may also like

Blog url changed to https

I just changed the url of this blog to https://jensjaeger.com. TLS encryption is now the default for all request to this page. It might be possible that some image links on some articles are hard coded http. If you find such an error it would be nice if you leave me comment so i can

Read More

Good-by journizer.com

In 2006 I startet a travel website called journizer.com with some friends. The goal was to build the ultimative website for individual travelers. The side has some successes but never get the momentum to grow this thing to a successful startup. Our biggest problem was the vision to build the ultimative site for everything about

Read More