Java - What is the output: formatter and date time value?

Question

What is the output of the following code?

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

public class Main {
  public static void main(String[] args) {
    String s = DateTimeFormatter.ISO_DATE.format(LocalTime.now());
    System.out.println(s);

  }
}


Click to view the answer

A runtime error as a LocalTime does not contain date components

Note

The datetime object being formatted must contain the components as suggested by their names.

ISO_DATE formatter expects the presence of the date components.

It should be not used to format time-only objects such as a LocalTime.

Similarly, the ISO_TIME formatter should be used to format a LocalDate.

Related Quiz