Skip to content

Using PrettyTime With Groovy

TheServerSide just brought PrettyTime to my notice:

PrettyTime is an OpenSource time formatting library. Completely customizable, PrettyTime creates human readable, relative timestamps like those seen on Digg, Twitter, and Facebook. It’s simple, get started “right now!”

My natural instinct was to give it a quick tryout. Of course, I used Groovy for my ‘experiment’:

import com.ocpsoft.pretty.time.PrettyTime

public class Test {

static void main(args) {
    def d = new Date()
    def pt = new PrettyTime(d)

    println "Now: $d"
    [-3000, -100, -1, 1, 100, 1000].each { off ->
      def nd = d + off
      println "(${nd}): ${pt.format(nd)}"
    }
  }
}

This gives:

Now: Fri Jun 19 19:28:48 EST 2009
(Mon Apr 02 19:28:48 EST 2001): 8 years ago
(Wed Mar 11 19:28:48 EST 2009): 3 months ago
(Thu Jun 18 19:28:48 EST 2009): 1 day ago
(Sat Jun 20 19:28:48 EST 2009): 1 day from now
(Sun Sep 27 19:28:48 EST 2009): 3 months from now
(Thu Mar 15 19:28:48 EST 2012): 2 years from now

Pretty rudimentary at the moment, but i18n support is coming, for instance.

Sometimes, it’s little things like neat time formatting that can make the difference between users saying “great” or “gross.” This may be worth keeping an eye on…

[edit]
It didn’t take long before a Grails PrettyTime Plugin appeared. Pretty impressive!

Tags: ,

Java Enterprise Edition, JEE, JavaServer Pages, JSP, Tag Libraries, Servlets, Enterprise Java Beans, EJB, Java Messaging Service JMS, BEA Weblogic, JBoss, Application Servers, Spring Framework, Groovy, Grails, Griffon, Seam, Open Source, Service Oriented Architectures, SOA, Java 2 Standard Edition, J2SE