Dashboard > Confluence User Guides and Documents > ... > Confluence Internals > Date formatting with time zones
Date formatting with time zones Log In   View a printable version of the current page.

Added by mryall, last edited by mryall on Jan 17, 2007  (view change)
Labels: 

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().

DEMONSTRATION LICENSE - This Confluence site is for demonstration purposes only. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.6.1 Build:#916 Nov 09, 2007) - Bug/feature request - Contact Administrators