Java Date and Time - HackerRank Solution

Java Date and Time - Hacker Rank Solution

Hello Friends, How are you? Today I am going to solve the HackerRank Java Date and Time Problem with a very easy explanation. This is the 12th problem of Java on HackerRank. In this article, you will get more than one approach to solve this problem. So let's start-

{tocify} $title={Table of Contents}

Java Date and Time - Hacker Rank Solution

HackerRank Java Date and Time - Problem Statement

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.


month = 8
day = 14
year = 2017
The method should return MONDAY as the day on that date.

Function Description

Complete the findDay function in the editor below.

findDay has the following parameters:

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


  • 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.


  • 2000 < year < 3000

Sample Input:

08 05 2015 {codeBox}

Sample Output:


Java Date and Time - Hacker Rank Solution

Approach I:

import java.math.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import static;
import static;

import static;
import static;
import java.time.LocalDate;

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) {
        int d = Integer.valueOf(day);
        int m = Integer.valueOf(month);
        int y = Integer.valueOf(year);
        LocalDate date = LocalDate.of(y, m, d);
        return date.getDayOfWeek().toString();


public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(;
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

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

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

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

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

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



Approach II:

import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

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

			Calendar calendar = Calendar.getInstance();
			calendar.set(Calendar.DATE, day);
			calendar.set(Calendar.MONTH, month - 1);
			calendar.set(Calendar.YEAR, year);

					Calendar.LONG, new Locale("en", "US")).toUpperCase());

Approach III:

import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import java.util.Scanner;

public class JavaDateAndTime {
    static List<String> days = Arrays.asList("SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY");
    public static String getDay(String day, String month, String year) {
        int y = Integer.parseInt(year);
        int m = Integer.parseInt(month);
        int d = Integer.parseInt(day);
        Calendar c = Calendar.getInstance();
        c.set(y, m-1, d); 
        int p = c.get(Calendar.DAY_OF_WEEK);
        String s = days.get(p-1);
        return s;
    public static void main(String[] args) {
        Scanner in = new Scanner(;
        String month =;
        String day =;
        String year =;
        System.out.println(getDay(day, month, year));

Disclaimer: The above Problem (Java Date and Time) is generated by Hackerrank but the Solution is Provided by MyEduWaves. This tutorial is only for Educational and Learning purposes. Authority if any of the queries regarding this post or website fill the contact form.

I hope you have understood the solution to this HackerRank Problem. All these three solutions will pass all the test cases. Now visit the Java Date and Time HackerRank Problem and try to solve it again.

All the Best!

Post a Comment

Previous Post Next Post