Algorithm


Problem Name: Java Date and Time

Problem Link: https://www.hackerrank.com/challenges/java-date-and-time/problem?isFullScreen=true

In this HackerRank Functions in Java programming problem 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,getDay , which returns the day on that date. To simplify your task, we have provided a portion of the code in the editor.

Example

month = 8

day = 14

year = 2017

The method should return MONDAY 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 MM DD YYYY format.

Constraints

  • 2000 < Year < 3000

Sample Input

08 05 2015

Sample Output

WEDNESDAY

 

 

 

 

 

Code Examples

#1 Code Example with Java Programming

Code - Java Programming


import java.util.Scanner;
import java.util.*;

public class Solution{
    public static String getDay(String d, String m, String y){
        Calendar c = Calendar.getInstance();
        c.set(Integer.valueOf(y), Integer.valueOf(m) - 1, Integer.valueOf(d));
        
        String day = "";
        switch(c.get(Calendar.DAY_OF_WEEK)){
            case 1:
                day = "Sunday";
                break;
            case 2:
                day = "Monday";
                break;
            case 3:
                day = "Tuesday";
                break;
            case 4:
                day = "Wednesday";
                break;
            case 5:
                day = "Thursday";
                break;
            case 6:
                day = "Friday";
                break;
            case 7:
                day = "Saturday";
                break;
        }
        return day.toUpperCase();
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String month = in.next();
        String day = in.next();
        String year = in.next();
        
        System.out.println(getDay(day, month, year));
    }
}
Copy The Code & Try With Live Editor
Advertisements

Demonstration


Previous
[Solved] Java Datatypes in Java solution in Hackerrank - Hacerrank solution Java
Next
[Solved] Java Currency Formatter in Java solution in Hackerrank - Hacerrank solution Java