DateTimeFormatter.WithZone(ZoneId) Method

Definition

Returns a copy of this formatter with a new override zone.

[Android.Runtime.Register("withZone", "(Ljava/time/ZoneId;)Ljava/time/format/DateTimeFormatter;", "", ApiSince=26)]
public Java.Time.Format.DateTimeFormatter? WithZone (Java.Time.ZoneId? zone);
[<Android.Runtime.Register("withZone", "(Ljava/time/ZoneId;)Ljava/time/format/DateTimeFormatter;", "", ApiSince=26)>]
member this.WithZone : Java.Time.ZoneId -> Java.Time.Format.DateTimeFormatter

Parameters

zone
ZoneId

the new override zone, null if no override

Returns

a formatter based on this formatter with the requested override zone, not null

Attributes

Remarks

Returns a copy of this formatter with a new override zone.

This returns a formatter with similar state to this formatter but with the override zone set. By default, a formatter has no override zone, returning null.

If an override is added, then any instant that is formatted or parsed will be affected.

When formatting, if the temporal object contains an instant, then it will be converted to a zoned date-time using the override zone. Whether the temporal is an instant is determined by querying the ChronoField#INSTANT_SECONDS INSTANT_SECONDS field. If the input has a chronology then it will be retained unless overridden. If the input does not have a chronology, such as Instant, then the ISO chronology will be used.

If the temporal object does not contain an instant, but does contain an offset then an additional check is made. If the normalized override zone is an offset that differs from the offset of the temporal, then a DateTimeException is thrown. In all other cases, the override zone is added to the temporal, replacing any previous zone, but without changing the date/time.

When parsing, there are two distinct cases to consider. If a zone has been parsed directly from the text, perhaps because DateTimeFormatterBuilder#appendZoneId() was used, then this override zone has no effect. If no zone has been parsed, then this override zone will be included in the result of the parse where it can be used to build instants and date-times.

This instance is immutable and unaffected by this method call.

Java documentation for java.time.format.DateTimeFormatter.withZone(java.time.ZoneId).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to