Tom McIntyre
Tom McIntyre

Reputation: 3699

Freemarker date format - different on different machines?

I have a problem where my Freemarker template produces different date formats on different machines.

My Freemarker macro looks like this:

<#macro renderTextEntry textEntry>
    <div id="text_entry_${textEntry.id}" class="text_entry">
        <span id="text_entry_${textEntry.id}_date" class="text_entry_date">${textEntry.time?datetime}</span>:
        <div id="text_entry_${textEntry.id}_content" class="text_entry_content"><#noescape>${textEntry.text}</#noescape></div>
        <button class="edit_text_entry">Edit</button>
        <a class="delete_text_entry" href="/delete-text-entry?id=${textEntry.id}"><img
                src="images/delete.png" alt="delete text entry"/></a>
    </div>
</#macro>

I have a unit test for the outcome, with the expected outcome being this:

<div id="text_entry_5" class="text_entry">
    <span id="text_entry_5_date" class="text_entry_date">Jan 2, 1970 11:17:36 AM</span>:
    <div id="text_entry_5_content" class="text_entry_content">some text</div>
        <button class="edit_text_entry">Edit</button>
        <a class="delete_text_entry" href="/delete-text-entry?id=5">
            <img src="images/delete.png" alt="delete text entry"/>
        </a>
</div>

One one machine this test passes. On another, it fails. The only difference is that the date is output as

02-Jan-1970 11:17:36

TextEntry.getDate() returns a java.util.Date object.

Both machines are MacbookPros running OSX 10.7.5 and Java 1.7.0_10-b18. I am not aware of any environment variables I have set that affects the default DateFormat - simply running

System.out.println(new Date());

produces the same output on both machines. I have no idea what is going on here - any help would be greatly appreciated!

NOTE: I have solved my problem by explicitly declaring the date format I want - the relevant Freemarker snippet is now

${textEntry.time?string("MMM d, yyyy HH:mm:ss a")}

However, it would still be nice to understand what is going on here!

Upvotes: 2

Views: 4785

Answers (2)

micha
micha

Reputation: 49582

Maybe you are using another browser language on the other computer?

As far as i know the default date formatting depends on the locale beeing used.

Upvotes: 2

ddekany
ddekany

Reputation: 31152

Since the date/time format is different depending on your nationality, the default date/time format of FreeMarker depends on FreeMarker's locale setting. See http://freemarker.org/docs/pgui_config_settings.html about FreeMarker settings. As you can see, the locale setting can be specified at various levels, and it's preferably done outside the templates. If your application is not internationalized, you probably want to set it in the FreeMarker Configuration object with setLocale(Locale). Otherwise you probably want to do the same on the Environment-level, so that it fits the locale of the current visitor.

Since you haven't specified the locale anywhere, FreeMarker has used the default locale picked by Java, which is usually the default locale of the OS (of the Web server). As of Date.toString, it's for debugging mostly, for programmer eyes, and so it ignores locale, that's why it always prints the same.

Note that if the format associated with the locale is still not what you want, the date and time format can be fine-tuned with the date_format, time_format and datetime_format settings of FreeMarker. Using ?string('...') is only the last resort.

Upvotes: 2

Related Questions