Confluence Docs 3.0 : Date formatting with time zones This page last changed on Jan 16, 2007 by mryall. ### Introduction Confluence 2.3 supports a time zone preference for a user. This means all dates in the system must be formatted using the same process to appear in the user's time zone correctly. This document describes how dates are formatted in Confluence. It may be useful to plugin developers who need to format dates in a special way inside Confluence. ### DateFormatter The new class introduced in Confluence 2.3, DateFormatter, allows formatting in the user's timezone. See the full javadoc for details, but methods include: `String format(Date date)` – Formats the date and returns it as a string, using the date formatting pattern. `String formatDateTime(Date date)` – Formats the date and returns it as a string, using the date-time formatting pattern. `String formatServerDate(Date date)` – Same as format(Date), but doesn't perform time zone conversion. Most methods format the time in the user's time zone. The 'server' methods format the time in the server's time zone. ### Accessing the DateFormatter in Velocity In Velocity, using the DateFormatter is easy because it is in the Velocity context. In a normal Velocity template (*.vm), such as an action result, you might use it like this:
    $dateFormatter.format($action.myBirthdayDate)
If you want to use the DateFormatter in a Velocity decorator (*.vmd), such as a custom layout or theme, you need to access it via its getter on the action:
    $action.dateFormatter.format( $page.lastModificationDate )
### Accessing the DateFormatter in code The DateFormatter is constructed by the ConfluenceUserPreferences object, which can be obtained from the UserAccessor. The code below gives a demonstration:
    ConfluenceUserPreferences preferences = userAccessor.getConfluenceUserPreferences(user);
    DateFormatter dateFormatter = preferences.getDateFormatter(formatSettingsManager);
    System.out.println(dateFormatter.formatDateTime(date));
The `userAccessor` and `formatSettingsManager` are Spring beans which can be injected into your object. You can usually get the `user` from the context of your macro or plugin, or using AuthenticatedUserThreadLocal.getUser().

Document generated by Confluence on Nov 05, 2009 23:34