This server will be upgraded at 3pm Sydney time on December 3rd (December 2nd, 8pm PST) and will be down for up to 30 minutes.
This documentation relates to the latest version of Confluence.
If you are using an earlier version, please go to the documentation home page and select the relevant version.

Date formatting with time zones

All Versions
Click for all versions
Confluence 2.10 Documentation

Index

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

Labels

date date Delete
format format Delete
confluence confluence Delete
timezone timezone Delete
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.