Given the following code fragment:
String pattern = "dd MMM yyyy HH:mm";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
LocalDate x = LocalDate.of(2022, 11, 1);
System.out.println(formatter.format(x));
What is the result if the default locale is en_US? Choose one.
* 01 Nov 2022 is printed.
* 01 11 2022 is printed.
* 01 N 2022 is printed.
* 01 November 2022 is printed.
* Compilation fails.
* A runtime exception occurs.
#java #certificationquestion #ocp
Answer:
A LocalDate does not carry any time information, so an attempt to access these values cannot be fulfilled in any sensible way.
Rather than present dummy values, the implementation of LocalDate throws an exception in response to requests it cannot properly fulfill.
Exception in thread "main" java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: HourOfDay
Therefore, A runtime exception occurs.
Note:
The generalization TemporalAccessor, which is an interface, declares the same access methods regardless of the capabilities of the implementing class.
Therefore, you can conclude that any code that attempts to access potentially unsupported fields through this interface will not generate compilation errors.
This means that option "Compilation fails." is incorrect.