Format a Date in Words

Problem

Read a date in the form YYYY-MM-DD. Print it in the form DD Month YYYY, for example 05 March 2024 (the day has two digits and the month is written in English).

Constraints

  • Years 1900 to 2100

Examples

Input

2024-03-05

Output

05 March 2024

Solutions

Best approachTime O(1)Space O(1)

Java's DateTimeFormatter uses a pattern language. Always pass a Locale, because the month name depends on the language of the computer. The other languages here use a month list, which gives the same result on every computer.

Same code works in Java810172125Basic features only, so nothing changes between versions.
/*
 * Problem : Work with dates in the form YYYY-MM-DD.
 * Input   : Dates as YYYY-MM-DD.
 * Output  : The date in words.
 * Approach: Format with a pattern.
 * Time    : O(1)
 * Space   : O(1)
 * Java    : 8 or newer.
 */
import java.util.Scanner;

public class Main {
    // This method solves the task. It receives the input and returns the answer.
    static String solve(String s) {
        // Step 1: A formatter describes the output shape. dd = day with two digits, MMMM = full month name, yyyy = year.
        // Step 2: Locale.ENGLISH makes sure the month name is in English on every computer.
        java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter.ofPattern("dd MMMM yyyy", java.util.Locale.ENGLISH);

        // Step 3: Read the date and format it.
        return java.time.LocalDate.parse(s.trim()).format(formatter);
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s = scanner.hasNextLine() ? scanner.nextLine() : "";
        System.out.println(solve(s));
    }
}
Hints (1)
  1. Why pass Locale.ENGLISH?