# 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-

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

Example:

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

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 {codeBox}

Sample Output:

WEDNESDAY {codeBox}

### Java Date and Time - Hacker Rank Solution

Approach I:

``````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.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
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 {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

int month = Integer.parseInt(firstMultipleInput);

int day = Integer.parseInt(firstMultipleInput);

int year = Integer.parseInt(firstMultipleInput);

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

bufferedWriter.write(res);
bufferedWriter.newLine();

bufferedWriter.close();
}
}``````

Approach II:

``````import java.io.*;
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(System.in)) {
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);

System.out.println(calendar.getDisplayName(Calendar.DAY_OF_WEEK,
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;

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(System.in);
String month = in.next();
String day = in.next();
String year = in.next();

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!