📍 생각대로 코딩
- 모든 달이 28일까지 있다고 가정하므로 Date format의 날짜 계산을 다르게 해야한다.
import java.util.*;
import java.text.*;
class Solution {
public int[] solution(String today, String[] terms, String[] privacies) {
ArrayList<String> answer_list = new ArrayList<String>();
HashMap<String,String> terms_map = new HashMap<String,String>();
for(int i=0; i<terms.length; i++) {
String[] tmp = terms[i].split(" ");
terms_map.put(tmp[0], tmp[1]);
}
try{
DateFormat format = new SimpleDateFormat("yyyy.MM.dd");
for(int i=0; i<privacies.length; i++) {
String[] tmp = privacies[i].split(" ");
int months = Integer.parseInt(terms_map.get(tmp[1]));
Date date = format.parse(tmp[0]);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MONTH, months);
Calendar cal2 = Calendar.getInstance();
cal2.setTime(format.parse(today));
if(cal2.after(cal) || cal2.equals(cal)) {
answer_list.add(String.valueOf(i+1));
}
}
} catch(Exception e){
System.out.println(e.getMessage());
}
int answer_list_size = answer_list.size();
String[] arr = answer_list.toArray(new String[answer_list_size]);
int[] answer = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
answer[i] = Integer.parseInt(arr[i]);
}
return answer;
}
}