Wednesday, September 7, 2011

Pretty Time - timestamp formatting made easy

Today I was looking for an utility to help me with formating timestamps to user friendly string values like: "moments ago", "few minutes ago", "in couple minutes" etc. Why to write my own implementation, when for sure someone already did it before? And did it better? While googling ("pretty+time+in+java") I found Pretty Time library and I tried it out.


With the maven it is just a second to have it in the project. You only add following dependency to the POM file:
<dependency>
 <groupid>com.ocpsoft</groupid>
 <artifactid>ocpsoft-pretty-time</artifactid>
 <version>1.0.7</version>
</dependency>
To format a date to a human readable value, one line of code is enough:
String prettyTimeString = new PrettyTime().format(new Date());
The result is: "chwilę temu" ("moments ago"). Just as expected.
Easy to use. Fully configurable. I18n support included. Just checkout the project page: http://ocpsoft.com/prettytime and enjoy the ease of timestamp formating!
Short note: If you wish to force formatting in English (default), you need to pass locale object to the c-tor with the empty language value, like below. If you don't do this you get the output in the language of the current locale. In my case it was Polish.
String prettyTimeString = new PrettyTime(new Locale("")).format(new Date());

0 komentarze:

Post a Comment