Oracle Certified Associate Java Se 8 Programmer · Free Practice Question Easy
Question 16
//DateParseTest.java
import java.time.LocalDate;
public class DateParseTest {
public static void main(String[] args) {
LocalDate date = LocalDate.parse("2018-1-01");
System.out.println(date);
}
}
What will be the result of compiling and executing the DateParseTest class?
-
A
An exception is thrown at runtime
-
B
2018-1-01
-
C
2018-1-1
-
D
2018-01-01
Reveal correct answer
Correct answer: A
Explanation
The LocalDate.parse(CharSequence) method accepts a String in the "9999-99-99" format only. Single-digit month and day values should be padded with 0 to convert them to 2 digits. If the correct format string is not passed, an instance of java.time.format.DateTimeParseException is thrown.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
