Formatting a Rhino JavaScript Date object

I’m working on a project which involves embedding JavaScript within Java. To make life easier for the the person creating the JavaScript, I added some syntactic sugar where the JavaScript way was cumbersome, complicated or did not exist. One of these was the formatting of the JavaScript Date object.

There’s no built in way to format a JavaScript Date object. While there are many implementations out there, I’d rather not to have to check someones blog for bug fixes and I’d also prefer not to have to load a 3rd party library if a solution exists in the runtime.

One of the cool features of JavaScript is that it’s prototype-based. It allows you to add methods to objects at run time. Java has the built in ability to easily format a Date as a String using the SimpleDateFormat class. Combining these two features allowed me to easily format a Date constructed in JavaScript as a String using Java’s SimpleDateFormat class and JavaScript’s method prototyping.

This solution has a couple of benefits. Using the built in Java classes means the documentation for the format mask is well written and readily available; and the code doing the formatting has been tested thoroughly.

Java code:

ScriptEngineManager scriptManager = new ScriptEngineManager();
ScriptEngine scriptEngine =
scriptEngine.eval(new FileReader("wrappers.js"));
scriptEngine.eval("println(new Date().format('yyyy-MM-dd'));");


var dateFormat = function (date, mask) {
    var formattedDate =
        new java.text.SimpleDateFormat(mask).format(date);
    return String(formattedDate);
Date.prototype.format = function (mask) {
    return dateFormat(this, mask);

Running the Java code on 10 August 2010 produces the following output:

Interesting to note is that the Date object passed to the SimpleDateFormat class is a JavaScript Date. The Rhino Engine will coerce the JavaScript Date into a Java Date when calling the format method on the SimpleDateFormat object. The following code in the NativeDateObject.coerceTypeImpl shows this conversion.

static Object coerceTypeImpl(Class type, Object value)
            else if (type == ScriptRuntime.DateClass
                     && value instanceof NativeDate)
                double time = ((NativeDate)value).getJSTimeValue();
                // XXX: This will replace NaN by 0
                return new Date((long)time);

So there you have it. Combining the best of both worlds to create an easy to use JavaScript Date formatter.

This entry was posted in Programming and tagged , , . Bookmark the permalink.

5 Responses to Formatting a Rhino JavaScript Date object

  1. Octavian Nita says:


    Thanks a lot for the very useful post! I got at least three ideas from it:
    – the coercion stuff (I was trying to find a way to manually do this – it’s true that I haven’t checked the Rhino docs too thoroughly or even tried the code to find out about it… mea culpa)
    – the very nice ideea of using JavaScript’s prototype mechanism
    – how to actually import/eval one javascript file into another in Rhino

    For me, the next step is to add sintactic sugar for importing now 🙂

    Regards, Tavi

  2. Jaxon says:

    This library is the most useful thing I’ve found when it comes to Date formatting in JavaScript:

    Not only does it make formatting a piece of cake, but it has some other cool features as well!

    I have no idea why it is listed as a jQuery plugin. I went through the code and couldn’t find any references to anything that looked like jQuery (or any other libraries for that matter.) I’ve used it standalone and it works just fine.

  3. Graham Perry says:

    All these years later and still totally relevant for us poor unlucky sods who still have to use Rhine SSJS engine. Thanks for posting this!

Leave a Reply

Your email address will not be published. Required fields are marked *