Java-Date-Time Hackerrank

 The Calendar class is an abstract class that provides methods for converting between a specific instant in time and a set of calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, and so on, and for manipulating the calendar fields, such as getting the date of the next week.


You are given a date. You just need to write the method, , which returns the day on that date. To simplify your task, we have provided a portion of the code in the editor.

Example


The method should return  as the day on that date.

image

Function Description

Complete the findDay function in the editor below.

findDay has the following parameters:

  • int: month
  • int: day
  • int: year

Returns

  • string: the day of the week in capital letters

Input Format

A single line of input containing the space separated month, day and year, respectively, in    format.

Constraints

Sample Input

08 05 2015

Sample Output

WEDNESDAY

Explanation

The day on August th  was WEDNESDAY.

Solution:

import java.io.*;

import java.math.*;

import java.security.*;

import java.text.*;

import java.util.*;

import java.util.concurrent.*;

import java.util.function.*;

import java.util.regex.*;

import java.util.stream.*;

import static java.util.stream.Collectors.joining;

import static java.util.stream.Collectors.toList;

import java.time.format.DateTimeFormatter;

import java.time.*;


class Result {

    /*

     * Complete the 'findDay' function below.

     *

     * The function is expected to return a STRING.

     * The function accepts following parameters:

     *  1. INTEGER month

     *  2. INTEGER day

     *  3. INTEGER year

     */

    public static String findDay(int month, int day, int year) {

        // Combine the input parameters into a date string

        String input = day + "-" + month + "-" + year;

        // Define the date format pattern

        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("d-M-u", Locale.ENGLISH);

        // Parse the date string into a LocalDate object

        LocalDate date = LocalDate.parse(input, dtf);

        // Get the day of the week for the given date

        DayOfWeek dow = date.getDayOfWeek();

        // Return the day of the week as a string in uppercase

        return dow.toString();

    }

}

public class Solution {

    public static void main(String[] args) throws IOException {

        // BufferedReader to read input from standard input

        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));

        // BufferedWriter to write output to standard output

        BufferedWriter bufferedWriter = new BufferedWriter(new   

                                                               FileWriter(System.getenv("OUTPUT_PATH")));

        // Read the input line and split it into components

        String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");

        // Parse the input components into integers

        int month = Integer.parseInt(firstMultipleInput[0]);

        int day = Integer.parseInt(firstMultipleInput[1]);

        int year = Integer.parseInt(firstMultipleInput[2]);

        // Call the findDay method to get the day of the week

        String res = Result.findDay(month, day, year);

        // Write the result to the output

        bufferedWriter.write(res);

        bufferedWriter.newLine();

        // Close the BufferedReader and BufferedWriter

        bufferedReader.close();

        bufferedWriter.close();

    }

}


Explanation of Code:

  • findDay Method:

    • Combines the input parameters (month, day, year) into a date string.

    • Defines the date format pattern using DateTimeFormatter.

    • Parses the date string into a LocalDate object.

    • Retrieves the day of the week from the LocalDate object and returns it as an uppercase string.

  • main Method:

    • Uses BufferedReader to read input from the user.

    • Splits the input string into its components (month, day, year).

    • Calls the findDay method with the parsed input values.

    • Writes the result (day of the week) to the output using BufferedWriter.

    • Closes the BufferedReader and BufferedWriter to release resources.


Conclusion

This approach efficiently determines the day of the week for a given date using Java's LocalDate and DateTimeFormatter classes. It showcases the use of modern Java date and time API for date manipulation and formatting.




**  Please do subscribe my blog for future updates and share with your friends, if you find this informative **

Comments

Popular Posts