Class DateFormatter
- java.lang.Object
-
- com.atlassian.confluence.core.DateFormatter
-
public class DateFormatter extends Object
Formats dates and times into a given timezone in a variety of formats. The actual formats are provided by theFormatSettingsManagerwhich is passed to the constructor. Also allows for formatting into arbitrary formats.This class is not thread-safe, primarily because DateFormat implementations are not synchronised. You should construct one instance of this class per thread or request.
TODO: Should support user locales
-
-
Constructor Summary
Constructors Constructor Description DateFormatter(TimeZone timeZone, FormatSettingsManager formatSettingsManager, LocaleManager localeManager)DateFormatter(TimeZone timeZone, FormatSettingsManager formatSettingsManager, LocaleManager localeManager)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Stringformat(int formatOption, Date date)Formats the date and returns it as a string with a formatOption defined in JDK.Stringformat(Date date)Formats the date and returns it as a string, using the date formatting pattern.StringformatBlogDate(Date date)Formats the date as a blog posting date and returns it as a string.StringformatDateFull(Date date)Formats the date and returns it as a string, usingDateFormat.FULLas the formatting pattern.StringformatDateTime(Instant instant)Same asformatDateTime(Date)but accepting Java 8InstantStringformatDateTime(Date date)Formats the date and returns it as a string, using the date-time formatting pattern.StringformatGivenString(String formatString, Date date)Formats the date and returns it as a string, using the given formatting pattern.static StringformatMillis(long millis)StringformatServerDate(Date date)Same asformat(Date), but doesn't perform time zone conversion.StringformatServerDateFull(Date date)Same asformatDateFull(Date), but doesn't perform time zone conversion.StringformatServerDateTime(Date date)Same asformatDateTime(Date), but doesn't perform time zone conversion.StringformatServerDateWithUserLocale(int formatOption, Date date)Same asformat(int, Date), but doesn't perform time zone conversion.StringformatTime(Date date)Formats the date and returns it as a string, using the time formatting pattern.StringformatTimeMedium(Date date)Formats the date and returns it as a string, usingDateFormat.MEDIUMas the time formatting pattern.CalendargetCalendar()StringgetCurrentDate()StringgetCurrentDateTime()StringgetDateForBlogPost(Date date)TimeZonegetTimeZone()
-
-
-
Constructor Detail
-
DateFormatter
public DateFormatter(TimeZone timeZone, FormatSettingsManager formatSettingsManager, LocaleManager localeManager)
-
DateFormatter
public DateFormatter(TimeZone timeZone, FormatSettingsManager formatSettingsManager, LocaleManager localeManager)
-
-
Method Detail
-
format
public String format(Date date)
Formats the date and returns it as a string, using the date formatting pattern. This format might be 'yyyy-MM-dd' or something similar.The
TimeZonespecified in the constructor is used to convert the date from server time to the user's local time.
-
formatDateTime
public String formatDateTime(Date date)
Formats the date and returns it as a string, using the date-time formatting pattern. This format might be 'yyyy-MM-dd HH:mm:ss' or something similar.The
TimeZonespecified in the constructor is used to convert the date from server time to the user's local time.
-
formatDateTime
public String formatDateTime(Instant instant)
Same asformatDateTime(Date)but accepting Java 8Instant- Since:
- 7.14
-
formatTime
public String formatTime(Date date)
Formats the date and returns it as a string, using the time formatting pattern. This format might be 'HH:mm:ss' or something similar.The
TimeZonespecified in the constructor is used to convert the date from server time to the user's local time.
-
formatTimeMedium
public String formatTimeMedium(Date date)
Formats the date and returns it as a string, usingDateFormat.MEDIUMas the time formatting pattern.The
TimeZonespecified in the constructor is used to convert the date from server time to the user's local time.
-
formatDateFull
public String formatDateFull(Date date)
Formats the date and returns it as a string, usingDateFormat.FULLas the formatting pattern.The
TimeZonespecified in the constructor is used to convert the date from server time to the user's local time.
-
format
public String format(int formatOption, Date date)
Formats the date and returns it as a string with a formatOption defined in JDK.The
TimeZonespecified in the constructor is used to convert the date from server time to the user's local time.
-
formatServerDate
public String formatServerDate(Date date)
Same asformat(Date), but doesn't perform time zone conversion.This is typically used for formatting the posting dates of news items, which must be represented the same for all users.
-
formatServerDateWithUserLocale
public String formatServerDateWithUserLocale(int formatOption, Date date)
Same asformat(int, Date), but doesn't perform time zone conversion.This is typically used for formatting the posting dates of news items, which must be represented the same for all users.
-
formatServerDateTime
public String formatServerDateTime(Date date)
Same asformatDateTime(Date), but doesn't perform time zone conversion.This is typically used for formatting the posting dates of news items, which must be represented the same for all users.
-
formatBlogDate
public String formatBlogDate(Date date)
Formats the date as a blog posting date and returns it as a string. The format might be 'dd MMM @ h:mm a' or something similar.This method does not apply the
TimeZonespecified in the constructor, but instead uses server's default time zone. A blog post date is part of its identifier, therefore it must be consistent for all users.
-
formatServerDateFull
public String formatServerDateFull(Date date)
Same asformatDateFull(Date), but doesn't perform time zone conversion.This is typically used for formatting the posting dates of news items, which must be represented the same for all users.
-
formatGivenString
public String formatGivenString(String formatString, Date date)
Formats the date and returns it as a string, using the given formatting pattern.The
TimeZonespecified in the constructor is used to convert the date from server time to the user's local time.
-
getCurrentDateTime
public String getCurrentDateTime()
- Returns:
- the current date and time formatted as per
formatDateTime(Date)
-
getCurrentDate
public String getCurrentDate()
- Returns:
- the current date formatted as per
format(Date)
-
getDateForBlogPost
public String getDateForBlogPost(Date date)
- Returns:
- the given date time in editor blog post format
-
getCalendar
public Calendar getCalendar()
- Returns:
- a Calendar for the
TimeZonespecified in the constructor.
-
getTimeZone
public TimeZone getTimeZone()
- Returns:
- the
TimeZoneprovided when this formatter was created. - Since:
- 2.7
-
formatMillis
public static String formatMillis(long millis)
- Returns:
- a String representing the millisecond value in the format '[h:]mm:ss.S'. That is, hours will only be displayed if non-zero and any trailing zeroes in the milliseconds are truncated.
-
-