Day of the Week of a Date

Problem

Read a date in the form YYYY-MM-DD. Print the name of the day of the week (Monday, Tuesday, ...).

Constraints

  • Years 1900 to 2100

Examples

Input

2000-01-01

Output

Saturday

Solutions

Best approachTime O(1)Space O(1)

The day of the week follows from the calendar. The libraries number the days differently (Java uses an enum, JavaScript starts at Sunday = 0, Python starts at Monday = 0), so we map the number to a name carefully.

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 day name.
 * Approach: Ask the date library.
 * 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: Read the date. trim removes spaces around the text.
        java.time.LocalDate date = java.time.LocalDate.parse(s.trim());

        // Step 2: getDayOfWeek returns an enum like MONDAY. We change it to "Monday".
        String name = date.getDayOfWeek().name();
        return name.charAt(0) + name.substring(1).toLowerCase();
    }

    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. Which number does Sunday have in JavaScript?