알고리즘/프로그래머스
2016년
개발생각11
2020. 8. 31. 20:20
반응형
프로그래머스: 2016년 문제
https://programmers.co.kr/learn/courses/30/lessons/12901
풀이 방식:
입력받은 a월 b일과 1월 1일 금요일을 기준으로 날짜수의 차이를 구한 후, 7로 나눈 다음 나머지 값으로 days[] 배열에서 해당 요일 조회
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
class Solution {
public String solution(int a, int b) {
String answer = "";
String date1 = "2016-01-01";
String date2 = "2016-"+a+"-"+b;
String[] days = {"FRI", "SAT", "SUN", "MON", "TUE", "WED", "THU"};
try {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date firstDate = format.parse(date1);
Date secondDate = format.parse(date2);
long timeDiff = secondDate.getTime() - firstDate.getTime();
long calTimeDiff = timeDiff / (24*60*60*1000);
int remainder = (int)calTimeDiff % 7;
answer = days[remainder];
}catch(ParseException e){
}
return answer;
}
}
[Github]
반응형