Confluence : Date formatting with time zones
This page last changed on Jan 16, 2007 by mryall.
IntroductionConfluence 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. DateFormatterThe new class introduced in Confluence 2.3, DateFormatter, allows formatting in the user's timezone. See the full javadoc for details, but methods include:
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 VelocityIn 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 codeThe 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 Mar 22, 2007 21:00 |