Social date and time formatting

July 25, 2014 2 minutes reading time Development java

I don’t say “when I was on vacation on May 25th, 2013 at 12:13, I enjoyed it”. What I really enjoyed was my vacation last year. It’s the same with comments for blog posts. We do not need the exact date and time a comment was posted.

In 2009 I was looking for a Java library for a data warehouse project at a bank that displays time differences in a friendly way. As we say: “10 minutes ago”, “5 years ago” or “in 2 weeks” instead of “2 hours, 37 minutes and 11 seconds ago” or “17 August 2001” - unless it’s a rocket launch or a private anniversary whose exact date should not be forgotten. I found what I was looking for with the open-source project PrettyTime. Unfortunately, only English was supported at that time. I had to make the changes necessary to support German, because that was the language used for our project.

import org.ocpsoft.prettytime.PrettyTime

val pt = PrettyTime()
val ptString = pt.format(Date())

$ moments from now

The author was looking for someone to make his library fit for other languages. The requirement was that language resources can be easily stored on the classpath. The language should either be automatically recognized depending on the current locale, or set programmatically. Since I had made the language-specific adjustments already I made them available for PrettyTime.

When you write a comment for one of the posts on this blog, the time difference will be displayed with the help of PrettyTime. Go ahead, try it for yourself.