Java SE API
DateTimeFormatter.withResolverStyle()
Return a formatter with the requested resolver style. Java SE API; example uses Java 21-compatible syntax.
Formal Syntax & Structure
java syntaxjava
DateTimeFormatter withResolverStyle(ResolverStyle resolverStyle)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| resolverStyle | ResolverStyle | Date resolution policy |
Return Value
DateTimeFormatter
Return a formatter with the requested resolver style.
Working Example
Example Snippetjava
import java.math.*;import java.time.*;import java.time.format.*;import java.time.temporal.*;import java.text.*;import java.util.*;public class Main { public static void main(String[] args) throws Exception { var formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm",Locale.ROOT).withZone(ZoneId.of("Europe/Stockholm")); System.out.println(formatter.format(Instant.parse("2024-06-01T23:30:00Z"))); var strict = DateTimeFormatter.ofPattern("uuuu-MM-dd", Locale.ROOT).withResolverStyle(ResolverStyle.STRICT); System.out.println(LocalDate.parse("2024-02-29",strict)); try { LocalDate.parse("2023-02-29",strict); } catch(DateTimeParseException ex) { System.out.println("invalid date"); } }}