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.
Question
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.
Solution
Here is a step-by-step guide to solve this problem in Java:
- Import the necessary libraries:
import java.util.Calendar;
import java.text.SimpleDateFormat;
- Define the
findDayfunction:
public static String findDay(int month, int day, int year) {
Calendar calendar = Calendar.getInstance();
calendar.set(year, month - 1, day);
SimpleDateFormat format = new SimpleDateFormat("EEEE");
String dayOfWeek = format.format(calendar.getTime()).toUpperCase();
return dayOfWeek;
}
Here's what the function does:
- It first creates an instance of the
Calendarclass. - Then it sets the date of this calendar instance to the given date.
- A
SimpleDateFormatobject is created with the format "EEEE", which stands for day of the week in full form. - The
formatmethod ofSimpleDateFormatclass is then used to format the date of the calendar instance into the day of the week. - The
toUpperCasemethod is used to convert the day of the week to uppercase. - Finally, the day of the week is returned.
- To test the function, you can use the following code:
public static void main(String[] args) {
System.out.println(findDay(8, 5, 2015)); // Outputs: WEDNESDAY
}
This will print "WEDNESDAY" to the console, which is the day of the week for August 5, 2015.
Similar Questions
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]; }}
Suppose we want to design a class Day for working with calendar days (such as January 1, 2016). Specify methods for the following operations.1)A getMonth method that gets the month as an integer between 1 and 12.CheckShow answer2)A getMonthName method that gets the month as a string, such as "May".CheckShow answer3)An addDays method that returns the calendar day that is n days from this one.CheckShow answer4)A moveByDays method that moves this calendar day object by n days.CheckShow answer5)A constructor that constructs a Day with year y, month m (as an integer between 1 and 12), and day of month d.
Create a class named Year that contains a data field that holds the number of days in theyear. Include a get method that displays the number of days and a constructor that setsthe number of days to 365. Create a subclass named LeapYear. LeapYear’s constructoroverrides Year’s constructor and sets the day field to 366.Write a program that instan6ates one object of each class and displays their data.Add a method named daysElapsed() to the Year class you created in Exercise 5a. ThedaysElapsed() method accepts two arguments represen6ng a month and a day; it returnsan integer indica6ng the number of days that have elapsed since January 1 of the year.Create a daysElapsed() method for the LeapYear class that overrides the method in theYear class. Write a program that calculates the days elapsed on March 1 for a Year and fora LeapYear.
Which of the following functions returns the day of the week from a given date in Excel? Answer( Please choose a correct answer ) CLEARDAY()WEEKDAY()MONTH()YEAR()
Write a function 'returnDay' that takes one parameter(number from 1 to 7) and returns the day of the week.(where 1 is Monday, 2 is Tuesday, 3 is Wednesday, etc).If the number is less than 1 or greater than 7, the function should return null.Note: Store the days of the week in the array.
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.