blog.itcode.devblog.itcode.dev

[Programmers / JAVA] Level 1 2016 (12901)

January 1, 2016 was a Friday. What day of the week is month a, day b of 2016? Given two numbers a and b, complete the function solution that returns which day of the week it is on 2016 month a, day b. The day names are SUN, MON, TUE, WED, THU, FRI, and SAT, from Sunday to Saturday. For example, if a = 5 and b = 24, May 24th is a Tuesday, so return the string "TUE".

[Programmers / JAVA] Level 1 2016 (12901)

January 1, 2016 was a Friday. What day of the week is month a, day b of 2016? Given two numbers a and b, complete the function solution that returns which day of the week it is on 2016 month a, day b. The day names are SUN, MON, TUE, WED, THU, FRI, and SAT, from Sunday to Saturday. For example, if a = 5 and b = 24, May 24th is a Tuesday, so return the string "TUE".
RWB0104
@RWBwritten at 2021-12-15 13:58:49
Programmers

시리즈 모아보기

Programmers

26 / 78
RankLanguage Used
Level 1

🖼️ JAVA

🔗 2016

January 1, 2016 was a Friday. What day of the week is month a, day b of 2016? Given two numbers a and b, complete the function solution that returns which day of the week it is on 2016 month a, day b. The day names are SUN, MON, TUE, WED, THU, FRI, and SAT, from Sunday to Saturday. For example, if a = 5 and b = 24, May 24th is a Tuesday, so return the string "TUE".

  • 2016 is a leap year.
  • 2016 month a, day b is a date that actually exists. (Dates like month 13, day 26, or month 2, day 45 will not be given.)
abresult
524"TUE"

This problem asks for the day of the week corresponding to a specific date in 2016. Using a date object, you can find it easily with no extra calculation needed.

Here, we'll use the Date object to convert a string in the form yyyy-MM-dd into a Date object, and extract the day of the week from it.

You can extract the day of the week with Date.valueOf("yyyy-MM-dd").toLocalDate().getDayOfWeek().getValue().

Fortunately, Monday starts at 1 and it ends at 7 for Sunday. Depending on the language or locale, sometimes 1 is treated as Sunday, or 0 as either Monday or Sunday.

To simplify the code, we implement a method that takes a number between 1 and 7 and returns the corresponding day name.

JAVA

import java.sql.Date;

/**
 * 2016 class
 *
 * @author RWB
 * @since 2021.12.12 Sun 03:38:13
 */
class Solution
{
	/**
	 * Method that returns the answer
	 *
	 * @param a: [int] month
	 * @param b: [int] day
	 *
	 * @return [String] answer
	 */
	public String solution(int a, int b)
	{
		String dateStr = new StringBuilder().append("2016-").append(a).append("-").append(b).toString();
		
		int dayOfWeek = Date.valueOf(dateStr).toLocalDate().getDayOfWeek().getValue();
		
		return getDayName(dayOfWeek);
	}
	
	/**
	 * Method that returns the day name
	 *
	 * @param dayOfWeek: [int] day-of-week number
	 *
	 * @return [String] day name
	 */
	private String getDayName(int dayOfWeek)
	{
		return switch (dayOfWeek)
				{
					case 1 -> "MON";
					case 2 -> "TUE";
					case 3 -> "WED";
					case 4 -> "THU";
					case 5 -> "FRI";
					case 6 -> "SAT";
					default -> "SUN";
				};
	}
}
# Programmers# Algorithm# JAVA# Level 1
ship
blog.itcode.dev

Notes from the π-th Alpaca

7.0.1
Developed by RWB since 2021.057th upgraded at 2026.08