Date formatting with time zones
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.
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:
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:
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:
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().