Java Date and Time Solution


Problem Overview

Original Problem: Java Date and Time | Hackerrank

Difficulty: Easy

Category: Date Manipulation

Description: Our task is to write the findDay function, which accepts three integers representing a date (month, day, and year), and returns the name of the day of the week (like "MONDAY", "TUESDAY") that corresponds to that date.

Understanding the Problem

This problem is about taking the three provided integers and then manipulating those numbers in a date format to determine the weekday of that date. 

Input:

A single line of space-separated integers in the format of MM DD YYYY

Output:

A single word representing the day of the week, printed in uppercase

Initial Reasoning

The original problem provides a prewritten main() method, which handles reading the input and printing the output. This allows us to focus solely on implementing the findDay() function.

Since this is a date-related problem, we can use existing classes designed for this task, such as the LocalDate class from the java.time package. This will allow us to simply convert the integer input into a date and then retrieve the day of the week.

Pseudocode

// Main function is already provided

// findDay function:
	Create a LocalDate object using the year, month, and day
	Retrieve the day of the week from the date object
	Return the day of the week as uppercase string

Solution Code

// Note this is one of many possible solutions
import java.time.LocalDate;
import java.time.DayOfWeek;
import java.util.Scanner;

class Result {

    /**
     * This function takes a date (month, day, year)
     * and returns the name of the day of the week it falls on.
     */
    public static String findDay(int month, int day, int year) {
        // Create a LocalDate object using the given year, month, and day
        LocalDate date = LocalDate.of(year, month, day);
        
        // Get the day of the week (e.g., MONDAY, TUESDAY, etc.)
        DayOfWeek dayOfWeek = date.getDayOfWeek();
        
        // Convert to uppercase string and return
        return dayOfWeek.toString();
    }
}

public class Solution {

    // This is provided with the original problem
    public static void main(String[] args) throws IOException {
        // Create BufferedReader for reading input from the user
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));

        // Create BufferedWriter to write output to file
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        // Read the input line and remove trailing whitespaces, then split it into tokens
        String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");

        // Convert input strings to integers for month, day, and year
        int month = Integer.parseInt(firstMultipleInput[0]);
        int day = Integer.parseInt(firstMultipleInput[1]);
        int year = Integer.parseInt(firstMultipleInput[2]);

        // Call the findDay function with the parsed input
        String res = Result.findDay(month, day, year);

        // Write the result to the output file
        bufferedWriter.write(res);
        bufferedWriter.newLine();

        // Close the input/output streams
        bufferedReader.close();
        bufferedWriter.close();
    }
}