You are given a date. You just need to write the method getDay which returns the day on that date - Hacker Rank Solution.

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.

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  MM DD YYYY  format.

Sample Input

08 05 2015

Sample Output

WEDNESDAY

Explanation

The day on August 5th 2015 was WEDNESDAY.


SOLUTION : 

import java.util.*;
import java.text.*;

public class Solution {

    public static String findDay(int month, int day, int year) {
        Calendar cal = Calendar.getInstance();
        cal.set(year, month - 1, day); // Month is 0-based in Calendar

        String[] days = {"SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY",
                         "THURSDAY", "FRIDAY", "SATURDAY"};
       
        return days[cal.get(Calendar.DAY_OF_WEEK) - 1];
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int month = in.nextInt();
        int day = in.nextInt();
        int year = in.nextInt();

        System.out.println(findDay(month, day, year));
    }
}



Popular posts from this blog

When a method in a subclass overrides a method in superclass, it is still possible to call the overridden method using super keyword - Hacker Rank Solution.

Java's System.out.printf function can be used to print formatted output. The purpose of this exercise is to test your understanding of formatting output using printf. To get you started, a portion of the solution is provided for you in the editor; you must format and print the input to complete the solution.