Tags

,

I started using this new Web Framework Play few weeks back and came across the requirement to decide if to use java.util.Date or Joda-time API. I started with java.util.date as it was a simple tryout of writing a small CRUD application in Play, but eventually moved on to Joda-time when more functionality was needed out of the date fields ( I should have thought that from starting ).

But the moving from java.util.Date to Joda-time had some side effects, and I thought there should be some easy solutions to those. One of the problem was, Play has these cool Java extensions  format(pattern)  and format(pattern,language) which you can use with the Date object in the view templates directly.

${new Date(1275910970000).format('dd MMMM yyyy hh:mm:ss')}
07 June 2010 01:42:50

And when you use Joda-time you don’t have these extension, but the good part about Play is that it is equally easy to create new extensions. Just create a new class which extends play.templates.JavaExtensions and define a method and you are good to go.

So lets start with first using Joda-time API in your models and then creating some Java extensions for Joda-time


Using Joda-time in Models

Annotate the Joda-time fields with @Type annotation and specify a type which hibernate uses to understand how to save this field in database

@Type(type="org.joda.time.contrib.hibernate.PersistentDateTime")
public DateTime postedAt = new DateTime();

For this to work, you also need a library joda-time-hibernate which you can download fromthis link. After you have the jar file, copy it to the lib folder and restart the application once to have that jar loaded.

Creating Play Extension methods for Joda-time

Start with creating a new package “helper”/”extensions” under app folder. After that you create a new class which extends play.templates.JavaExtensions. The first argument to the method would be the object you want to create extension for, in this case it would be the Joda DateTime object. All the following arguments would become the argument to your extension.

Here we will create two extension to Joda Datetime,

  1. dateTime.format() – This one will use the default “date.format” pattern which you can change in application.conf and is accessible using I18N.getDateFormat() in Java class.
  2. dateTime.format(String pattern) – In case you want to specify a pattern, then you can use this second version.
public class CustomJavaExtensions extends JavaExtensions {
        public static String format(DateTime dateTime) {
                return format(dateTime, I18N.getDateFormat());
        }
        public static String format(DateTime dateTime, String pattern) {
                DateTimeFormatter formatter = DateTimeFormat.forPattern(pattern);
                return formatter.print(dateTime);
        }
}

Using them in view templates

Once you have created a subclass for JavaExtension class, all the methods are immediately available in all the view templates to be used. So you can start using them as:

${post.postedAt.format()}
${post.postedAt.format('dd MMM yy')}

So you see, how easy it is to extend Play framework with extensions, which you can with the same easy use in your view templates. I am still enjoying playing with Play and will recommend it to everyone.

 

Advertisement