What was the day on the 10th of November in 1938?
2 min readNov 12, 2024
Return the day on a specific date
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.
import java.util.*;
public class Result {
public static String findDay(int month, int day, int year) {
// Create a Calendar instance
Calendar calendar = Calendar.getInstance();
// Set the calendar to the given date
//(month-1) because Calendar months are 0-indexed
// (i.e., January is 0, February is 1, etc.)
calendar.set(year, month - 1, day);
// Get the day of the week
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
System.out.println("dayOfWeek: " + dayOfWeek);
// Convert the day of the week to a String (1 = Sunday, 2 = Monday, etc.)
String[] days = { "SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY" };
// Return the day of the week in uppercase
// Calendar.DAY_OF_WEEK is 1-based (Sunday=1, Monday=2, etc.)
return days[dayOfWeek - 1];
}
public static void main(String[] args) {
int month = 11;
int day = 10;
int year = 1938;
System.out.println(findDay(month, day, year));
}
}
The 10th of November in 1938 was Thursday.
Let’s run and check the result.