ComplicationTextFormatting


class ComplicationTextFormatting


A utility for finding optimal, locale-aware date and time format patterns that are guaranteed to fit within the strict character limits of a ComplicationType.SHORT_TEXT field.

The primary challenge in formatting text for complications is that the length of formatted date/time strings can vary (e.g., "May" vs. "September"). This class solves this by systematically testing a set of preferred format skeletons against a range of date and time values. It determines the "worst-case" (longest) output for a given pattern and locale, ensuring the chosen pattern is always safe to use.

It also includes hardcoded overrides for specific locales where standard patterns are known to be too long.

Summary

Public constructors

Creates a new instance of ComplicationTextFormatting for the given locale.

Public functions

String?
getBestShortTextDateFormat(skeletons: Array<String>, fallback: String?)

Finds the best date format pattern from a prioritized list of skeletons that consistently produces strings short enough for SHORT_TEXT fields.

String?
getFormattedDayOfWeekForShortText(timeInMillis: Long, timeZone: TimeZone?)

Applies the short day-of-week formatting logic to a specific time.

String?
getFormattedTimeForShortText(
    timeInMillis: Long,
    timeZone: TimeZone?,
    use24Hour: Boolean
)

Applies the short text time formatting logic to a specific time.

Public properties

String

Returns a date format pattern for the full day-of-week name (e.g., "Tuesday").

String?

Returns a date format pattern for a short day-and-month representation (e.g., "Dec 25").

String?

Returns a date format pattern for the day of the month (e.g., "25").

String?

Returns a date format pattern for a short day-of-week representation (e.g., "Tue").

String?

Returns a date format pattern for a short month representation (e.g., "Dec").

String?

Returns a 12-hour time format pattern suitable for a short text field (e.g., "1:37").

String?

Returns a 12-hour time format pattern suitable for a short text field (e.g., "1:37 a.m.").

String?

Returns a 24-hour time format pattern suitable for a short text field (e.g., "13:37").

String?

Returns a 24-hour time format pattern suitable for a short text field (e.g., "13:37").

Public constructors

ComplicationTextFormatting

Added in 1.3.0-beta01
ComplicationTextFormatting(locale: Locale)

Creates a new instance of ComplicationTextFormatting for the given locale.

Parameters
locale: Locale

The Locale that defines the formatting conventions.

Public functions

getBestShortTextDateFormat

Added in 1.3.0-beta01
fun getBestShortTextDateFormat(skeletons: Array<String>, fallback: String?): String?

Finds the best date format pattern from a prioritized list of skeletons that consistently produces strings short enough for SHORT_TEXT fields.

It iterates through the provided skeletons in order. The first skeleton that passes the length-check across multiple date/time samples is selected. If no skeletons in the list produce a sufficiently short result, the fallback pattern is returned.

Parameters
skeletons: Array<String>

An array of date format skeletons to test, in order of preference. For example, ["MMMd", "MMd"] would try a three-letter month first before a two-digit month.

fallback: String?

The non-validated pattern to return if no skeleton in skeletons is suitable.

Returns
String?

The best-fitting date pattern string, or the fallback.

getFormattedDayOfWeekForShortText

Added in 1.3.0-beta01
fun getFormattedDayOfWeekForShortText(timeInMillis: Long, timeZone: TimeZone?): String?

Applies the short day-of-week formatting logic to a specific time.

Parameters
timeInMillis: Long

The UTC time to format, in milliseconds since the epoch.

timeZone: TimeZone?

The target TimeZone for the output string. null uses the system default.

Returns
String?

The final, formatted day-of-week string, ready for display.

getFormattedTimeForShortText

Added in 1.3.0-beta01
fun getFormattedTimeForShortText(
    timeInMillis: Long,
    timeZone: TimeZone?,
    use24Hour: Boolean
): String?

Applies the short text time formatting logic to a specific time.

Parameters
timeInMillis: Long

The UTC time to format, in milliseconds since the epoch.

timeZone: TimeZone?

The target TimeZone for the output string. null uses the system default.

use24Hour: Boolean

If true, formats in 24-hour time; otherwise, 12-hour.

Returns
String?

The final, formatted time string, ready for display.

Public properties

fullTextDayOfWeekFormat

Added in 1.3.0-beta01
val fullTextDayOfWeekFormatString

Returns a date format pattern for the full day-of-week name (e.g., "Tuesday"). This is intended for LONG_TEXT fields and is not length-checked.

shortTextDayMonthFormat

Added in 1.3.0-beta01
val shortTextDayMonthFormatString?

Returns a date format pattern for a short day-and-month representation (e.g., "Dec 25"). Tries skeletons in the order of MMMd, MMd, and Md. Falls back to d/MM.

shortTextDayOfMonthFormat

Added in 1.3.0-beta01
val shortTextDayOfMonthFormatString?

Returns a date format pattern for the day of the month (e.g., "25"). Tries skeletons dd and d. Falls back to dd.

shortTextDayOfWeekFormat

Added in 1.3.0-beta01
val shortTextDayOfWeekFormatString?

Returns a date format pattern for a short day-of-week representation (e.g., "Tue"). Tries skeletons EEE (abbreviation), EEEEEE (2-letter), and EEEEE (1-letter). Falls back to EEEEE.

shortTextMonthFormat

Added in 1.3.0-beta01
val shortTextMonthFormatString?

Returns a date format pattern for a short month representation (e.g., "Dec"). Tries skeletons in the order of MMM, MM, and M. Falls back to MM.

shortTextTimeFormat12Hour

Added in 1.3.0-beta01
val shortTextTimeFormat12HourString?

Returns a 12-hour time format pattern suitable for a short text field (e.g., "1:37"). This version may shorten the AM/PM marker (e.g., from "a.m." to "am") to meet length constraints.

shortTextTimeFormat12HourWithoutAmPmShortening

Added in 1.3.0-beta01
val shortTextTimeFormat12HourWithoutAmPmShorteningString?

Returns a 12-hour time format pattern suitable for a short text field (e.g., "1:37 a.m.").

This version preserves the full, locale-specific AM/PM marker (e.g., "a.m." with periods) and does not attempt to shorten it to save space.

shortTextTimeFormat24Hour

Added in 1.3.0-beta01
val shortTextTimeFormat24HourString?

Returns a 24-hour time format pattern suitable for a short text field (e.g., "13:37").

shortTextTimeFormat24HourWithoutAmPmShortening

Added in 1.3.0-beta01
val shortTextTimeFormat24HourWithoutAmPmShorteningString?

Returns a 24-hour time format pattern suitable for a short text field (e.g., "13:37").

Note: This is functionally identical to shortTextTimeFormat24Hour, as AM/PM shortening is not applicable to 24-hour formats.