# DynamicDateFormat

Artifact: [androidx.wear.protolayout:protolayout-expression](https://developer.android.com/jetpack/androidx/releases/wear-protolayout) [View Source](https://cs.android.com/search?q=file:androidx/wear/protolayout/expression/util/DynamicDateFormat.kt+class:androidx.wear.protolayout.expression.util.DynamicDateFormat) Added in [1.3.0](https://developer.android.com/jetpack/androidx/releases/wear-protolayout#1.3.0)

*** ** * ** ***

<br />

```
@RequiresSchemaVersion(major = 1, minor = 300)
public final class DynamicDateFormat
```

<br />

*** ** * ** ***

Equivalent to `https://developer.android.com/reference/android/icu/text/SimpleDateFormat.html`, but generates a `https://developer.android.com/reference/androidx/wear/protolayout/expression/DynamicBuilders.DynamicString` based on a `https://developer.android.com/reference/androidx/wear/protolayout/expression/DynamicBuilders.DynamicInstant`.

See `https://developer.android.com/reference/android/icu/text/SimpleDateFormat.html` documentation for the pattern syntax.

Literal patterns are fully supported, including quotes (`'`) or non-letters (e.g. `:`).

Currently this implementation only supports hour (`HKhk`), minute (`m`), and AM/PM (`a`) patterns. Every other letter will throw `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-argument-exception/index.html`.

NOTE: `https://developer.android.com/reference/androidx/wear/protolayout/expression/util/DynamicDateFormat` uses `Locale.getDefault(Locale.Category.FORMAT)` at the time of calling `https://developer.android.com/reference/androidx/wear/protolayout/expression/util/DynamicDateFormat#format(androidx.wear.protolayout.expression.DynamicBuilders.DynamicInstant)` for AM/PM markers. This can change on the remote side, which would cause a mismatch between the locally-formatted AM/PM and the remotely-formatted numbers, unless the provider sends a newly formatted `https://developer.android.com/reference/androidx/wear/protolayout/expression/DynamicBuilders.DynamicString` (using a new invocation of `https://developer.android.com/reference/androidx/wear/protolayout/expression/util/DynamicDateFormat#format(androidx.wear.protolayout.expression.DynamicBuilders.DynamicInstant)`).

Example usage:

```
// This statement:
DynamicDateFormat(pattern = "HH:mm", timeZone = zone).format(dynamicInstant)
// Generates an equivalent of:
dynamicInstant
  .getHour(zone)
  .format(DynamicInt32.IntFormatter.Builder().setMinIntegerDigits(2).build())
  .concat(DynamicString.constant(":"))
  .concat(
    dynamicInstant.getMinute(zone)
      .format(DynamicInt32.IntFormatter.Builder().setMinIntegerDigits(2).build())
  )
```

## Summary

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/wear/protolayout/expression/util/DynamicDateFormat#DynamicDateFormat(kotlin.String,java.time.ZoneId)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html pattern, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/time/ZoneId.html timeZone)` Constructs a `https://developer.android.com/reference/androidx/wear/protolayout/expression/util/DynamicDateFormat`. |

| ### Public methods |
|---|---|
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/wear/protolayout/expression/DynamicBuilders.DynamicString` | `@https://developer.android.com/reference/androidx/wear/protolayout/expression/RequiresSchemaVersion(major = 1, minor = 300) https://developer.android.com/reference/androidx/wear/protolayout/expression/util/DynamicDateFormat#format(androidx.wear.protolayout.expression.DynamicBuilders.DynamicInstant)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/wear/protolayout/expression/DynamicBuilders.DynamicInstant instant)` Formats the `https://developer.android.com/reference/androidx/wear/protolayout/expression/DynamicBuilders.DynamicInstant` into a date/time `https://developer.android.com/reference/androidx/wear/protolayout/expression/DynamicBuilders.DynamicString`. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/time/ZoneId.html` | `https://developer.android.com/reference/androidx/wear/protolayout/expression/util/DynamicDateFormat#getTimeZone()()` The time zone used when extracting time parts from the `https://developer.android.com/reference/androidx/wear/protolayout/expression/DynamicBuilders.DynamicInstant` provided to `https://developer.android.com/reference/androidx/wear/protolayout/expression/util/DynamicDateFormat#format(androidx.wear.protolayout.expression.DynamicBuilders.DynamicInstant)`, defaults to `https://developer.android.com/reference/java/time/ZoneId.html#systemDefault()`. |
| `final void` | `https://developer.android.com/reference/androidx/wear/protolayout/expression/util/DynamicDateFormat#setTimeZone(java.time.ZoneId)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/time/ZoneId.html value)` The time zone used when extracting time parts from the `https://developer.android.com/reference/androidx/wear/protolayout/expression/DynamicBuilders.DynamicInstant` provided to `https://developer.android.com/reference/androidx/wear/protolayout/expression/util/DynamicDateFormat#format(androidx.wear.protolayout.expression.DynamicBuilders.DynamicInstant)`, defaults to `https://developer.android.com/reference/java/time/ZoneId.html#systemDefault()`. |

## Public constructors

### DynamicDateFormat

Added in [1.3.0](https://developer.android.com/jetpack/androidx/releases/wear-protolayout#1.3.0)

```
public DynamicDateFormat(@NonNull String pattern, @NonNull ZoneId timeZone)
```

Constructs a `https://developer.android.com/reference/androidx/wear/protolayout/expression/util/DynamicDateFormat`.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html pattern` | The pattern to use when calling `https://developer.android.com/reference/androidx/wear/protolayout/expression/util/DynamicDateFormat#format(androidx.wear.protolayout.expression.DynamicBuilders.DynamicInstant)`, see `https://developer.android.com/reference/android/icu/text/SimpleDateFormat.html` for general syntax and `https://developer.android.com/reference/androidx/wear/protolayout/expression/util/DynamicDateFormat` for the supported subset of features. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/time/ZoneId.html timeZone` | The time zone used when extracting time parts from the `https://developer.android.com/reference/androidx/wear/protolayout/expression/DynamicBuilders.DynamicInstant` provided to `https://developer.android.com/reference/androidx/wear/protolayout/expression/util/DynamicDateFormat#format(androidx.wear.protolayout.expression.DynamicBuilders.DynamicInstant)`, defaults to `https://developer.android.com/reference/java/time/ZoneId.html#systemDefault()`. |

## Public methods

### format

Added in [1.3.0](https://developer.android.com/jetpack/androidx/releases/wear-protolayout#1.3.0)

```
@RequiresSchemaVersion(major = 1, minor = 300)
public final @NonNull DynamicBuilders.DynamicString format(@NonNull DynamicBuilders.DynamicInstant instant)
```

Formats the `https://developer.android.com/reference/androidx/wear/protolayout/expression/DynamicBuilders.DynamicInstant` into a date/time `https://developer.android.com/reference/androidx/wear/protolayout/expression/DynamicBuilders.DynamicString`.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/wear/protolayout/expression/DynamicBuilders.DynamicInstant instant` | The `https://developer.android.com/reference/androidx/wear/protolayout/expression/DynamicBuilders.DynamicInstant` to format, defaults to `https://developer.android.com/reference/androidx/wear/protolayout/expression/DynamicBuilders.DynamicInstant#platformTimeWithSecondsPrecision()`. |

### getTimeZone

Added in [1.3.0](https://developer.android.com/jetpack/androidx/releases/wear-protolayout#1.3.0)

```
public final @NonNull ZoneId getTimeZone()
```

The time zone used when extracting time parts from the `https://developer.android.com/reference/androidx/wear/protolayout/expression/DynamicBuilders.DynamicInstant` provided to `https://developer.android.com/reference/androidx/wear/protolayout/expression/util/DynamicDateFormat#format(androidx.wear.protolayout.expression.DynamicBuilders.DynamicInstant)`, defaults to `https://developer.android.com/reference/java/time/ZoneId.html#systemDefault()`.

### setTimeZone

Added in [1.3.0](https://developer.android.com/jetpack/androidx/releases/wear-protolayout#1.3.0)

```
public final void setTimeZone(@NonNull ZoneId value)
```

The time zone used when extracting time parts from the `https://developer.android.com/reference/androidx/wear/protolayout/expression/DynamicBuilders.DynamicInstant` provided to `https://developer.android.com/reference/androidx/wear/protolayout/expression/util/DynamicDateFormat#format(androidx.wear.protolayout.expression.DynamicBuilders.DynamicInstant)`, defaults to `https://developer.android.com/reference/java/time/ZoneId.html#systemDefault()`.