
내가 작성한 코드
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
class Solution {
public String solution(int a, int b) {
String answer = "2016-" + String.format("%02d", a) + "-" + String.format("%02d", b); // 날짜 문자열 만들기
LocalDate date = LocalDate.parse(answer, DateTimeFormatter.ofPattern("yyyy-MM-dd")); // "yyyy-MM-dd" 형식의 문자열을 LocalDate로 변환
DayOfWeek dayOfWeek = date.getDayOfWeek(); // 요일 정보 얻기
return dayOfWeek.toString().substring(0, 3);
}
}
다른 사람의 코드
class Solution {
public String solution(int a, int b) {
String answer = "";
int[] c = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; // 각 월의 일 수
String[] MM = { "FRI", "SAT", "SUN", "MON", "TUE", "WED", "THU" }; // 요일 배열
int Adate = 0; // 1월 1일부터 a월 b일까지의 날짜 수
// -1 하는 이유 >> 이번 달의 일 수를 더하지 않기 위해
for (int i = 0; i < a - 1; i++) {
Adate += c[i];
}
Adate += b - 1;
answer = MM[Adate % 7]; // 총 날짜에서 7로 나눈 나머지로 요일 결정
return answer;
}
}
Share article