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"));
Question
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"));
Solution
The code you provided is written in Java and it's using the Calendar class to set a specific date (May 7, 2015) and then print out some information about that date.
Here's a step-by-step explanation of what the code does:
-
Calendar cal = Calendar.getInstance();This line creates a new Calendar object that represents the current date and time. -
cal.set(Calendar.DAY_OF_MONTH, 7);This line sets the day of the month to 7. -
cal.set(Calendar.MONTH, 4);This line sets the month to 4. In the Calendar class, months are zero-based, so 4 represents May. -
cal.set(Calendar.YEAR, 2015);This line sets the year to 2015. -
System.out.println("Week of month " + cal.get(Calendar.WEEK_OF_MONTH));This line prints out the week of the month for the date that's been set. -
System.out.println("Day of week in month " + (cal.get(Calendar.DAY_OF_WEEK_IN_MONTH)));This line prints out the day of the week in the month for the date that's been set. -
System.out.println("Day " + cal.get(Calendar.DAY_OF_MONTH));This line prints out the day of the month for the date that's been set. -
System.out.println("Month " + (Calendar.MAY == cal.get(Calendar.MONTH) ? "May" : "April"));This line prints out the month for the date that's been set. It uses a ternary operator to print "May" if the month is May and "April" if it's not.
The output of the program will depend on the specific date and time when the program is run, but it will always print out the week of the month, the day of the week in the month, the day of the month, and the month for May 7, 2015.
Similar Questions
What will be the output of the following program? Calendar cal = Calendar.getInstance(); cal.set(Calendar.HOUR, 23); cal.set(Calendar.MINUTE, 58); cal.set(Calendar.SECOND, 59); System.out.print(cal.get(Calendar.HOUR) + ":"); System.out.print(cal.get(Calendar.MINUTE) + ":"); System.out.print(cal.get(Calendar.SECOND));
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]; }}
What will be the output when the following code is executed?public class Product {public static void main(String[] args) { LocalDate startLocalDate = LocalDate.of(2016, Month.JUNE, 01);LocalDate tempLocalDate = startLocalDate;startLocalDate.plusDays(10);System.out.println(tempLocalDate);}} 2016-06-0101-06-20162016-06-1111-06-2016
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
Solution.java:23: error: ')' expected System.out.printf("The day of the week is -> %s %n%n%n", "daysOfWeek[dayOfWeek]"; ^
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.