import java.util.Calendar;public class JavaDateAndTimeDaysOfWeekFinder { public static String findDay(int month, int day, int year) { System.out.println("_________________________ Java Date And Time Days Of Week Finder _________________________________"); System.out.printf("The day of the week is -> %d/%d/%d %n",day,month,year); Calendar calendar = Calendar.getInstance(); calendar.set(year, month-1, day); // note: months in Calendar are 0-based (0 = January, 1 = February, ..., 11=December) int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK); String[] daysOfWeek = {"", "SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY" }; System.out.printf("The day of the week is -> %s %n%n%n", "daysOfWeek[dayOfWeek]"; return daysOfWeek[dayOfWeek]; }}
Question
import java.util.Calendar;public class JavaDateAndTimeDaysOfWeekFinder { public static String findDay(int month, int day, int year) { System.out.println("_________________________ Java Date And Time Days Of Week Finder _________________________________"); System.out.printf("The day of the week is -> %d/%d/%d %n",day,month,year); Calendar calendar = Calendar.getInstance(); calendar.set(year, month-1, day); // note: months in Calendar are 0-based (0 = January, 1 = February, ..., 11=December) int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK); String[] daysOfWeek = {"", "SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY" }; System.out.printf("The day of the week is -> %s %n%n%n", "daysOfWeek[dayOfWeek]"; return daysOfWeek[dayOfWeek]; }}
Solution
This is a Java program that finds the day of the week for a given date. Here's a step-by-step explanation:
-
The program starts with the import statement
import java.util.Calendar;which imports the Calendar class from the java.util package. This class is used to get the current date and time. -
The
JavaDateAndTimeDaysOfWeekFinderclass is declared with a single static methodfindDay(int month, int day, int year). This method takes three parameters: the month, day, and year of the date you want to find the day of the week for. -
Inside the
findDaymethod, a Calendar instance is created withCalendar.getInstance();. -
The date is set on the Calendar instance with
calendar.set(year, month-1, day);. Note that months in the Calendar class are 0-based, so we subtract 1 from the month. -
The day of the week is then retrieved from the Calendar instance with
calendar.get(Calendar.DAY_OF_WEEK);. This returns an integer representing the day of the week, where 1 = Sunday, 2 = Monday, ..., 7 = Saturday. -
A String array
daysOfWeekis declared and initialized with the names of the days of the week. -
The name of the day of the week is then retrieved from the
daysOfWeekarray using the integer retrieved earlier. This is printed to the console and also returned by the method.
Please note that there is a syntax error in the print statement. It should be System.out.printf("The day of the week is -> %s %n%n%n", daysOfWeek[dayOfWeek]); instead of System.out.printf("The day of the week is -> %s %n%n%n", "daysOfWeek[dayOfWeek]";.
Similar Questions
Solution.java:23: error: ')' expected System.out.printf("The day of the week is -> %s %n%n%n", "daysOfWeek[dayOfWeek]"; ^
What will be the output of the following program? Calendar cal = Calendar.getInstance(); cal.set(Calendar.DAY_OF_MONTH, 7); cal.set(Calendar.MONTH, 4); cal.set(Calendar.YEAR, 2015); System.out.println("Week of month " + cal.get(Calendar.WEEK_OF_MONTH)); System.out.println("Day of week in month " + (cal.get(Calendar.DAY_OF_WEEK_IN_MONTH))); System.out.println("Day " + cal.get(Calendar.DAY_OF_MONTH)); System.out.println("Month " + (Calendar.MAY == cal.get(Calendar.MONTH) ? "May" : "April"));
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.ExampleThe method should return as the day on that date.Function DescriptionComplete the findDay function in the editor below.findDay has the following parameters:int: monthint: dayint: yearReturnsstring: the day of the week in capital lettersInput FormatA single line of input containing the space separated month, day and year, respectively, in format.ConstraintsSample Input08 05 2015Sample OutputWEDNESDAYExplanationThe day on August th was WEDNESDAY.
Give the output of the following code segment-public class Main {public static void main(String[] args) {int day = 4;switch (day) {case 6:System.out.println("Today is Saturday");break;case 7:System.out.println("Today is Sunday");break;default:System.out.println("Looking forward to the Weekend");}}
Select the correct answerWhat is the output of the following Java code snippet?int dayOfWeek = 3;switch (dayOfWeek) { case 1: System.out.println("Monday"); break; case 2: System.out.println("Tuesday"); break; case 3: System.out.println("Wednesday"); break; default: System.out.println("Other day");}OptionsWednesdayTuesdayMondayOther day
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.