백준 2730: 오늘은 OS 숙제 제출일
위 문제를 풀다가 날짜를 의미하는 문자열의 처리를 공부하기 위해 Java API Document 에서 관련한 클래스를 공부해보자. (물론 다른 방법으로도 풀 수 있다)
유사하게, 시간을 다루는 LocalDate class도 이미 다뤄봤다.
https://velog.io/write?id=fb4a75ce-e311-41a3-8829-68a98778ea53

다음과 같은 입력에서, 각 줄 당 첫 번째 날짜는 숙제 마감일이고, 두 번째 날짜는 숙제를 낸 날짜이다.
숙제를 마감기한보다 얼마나 빨리, 늦게 냈는지 등을 출력하는 문제이다.

현재 java 로 해당 문제를 맞춘 사람 중에서 내 제출이 가장 시간이 짧다.
난 세계 1등이다.
함정은 이 문제를 푼 사람이 총 12명 밖에 안 된다는 것이다.
12명 참가이지만 어쨌든 세계 1등이다.
문제는 다른 방법으로 풀었지만 논외로 날짜를 다루는 LocalDate API를 연습해보자.
LocalDate class 를 생성하는 static method
알고리즘, 코딩테스트 때 쓸만한 메서드만 정리해봤다.
- static LocalDate of(int year, int month, int dayOfMonth)
- Obtains an instance of LocalDate from a year, month and day.- static LocalDate ofYearDay(int year, int dayOfYear)
- Obtains an instance of LocalDate from a year and day-of-year.
- 해당 연도의 몇 번째 day 인지 계산해서 생성해준다.- static LocalDate parse(CharSequence text)
- Obtains an instance of LocalDate from a text string such as 2007-12-03
LocalDate Instance 를 처리하는 method
비교/값 얻기
- int compareTo(ChronoLocalDate other)
- Compares this date to another date.
- negative if less, positive if greater, 0 if equal
- int getDayOfMonth()
- 해당 월의 며칠 인지- DayOfWeek getDayOfWeek()
- 무슨 요일인지 Enum을 return
- int getDayOfYear()
- 해당 년의 몇 번째 날인지- int getMonthValue()
- Gets the month-of-year field from 1 to 12.- int getYear()
- Gets the year field.- boolean isAfter(ChronoLocalDate other)
- boolean isBefore(ChronoLocalDate other)
- boolean isEqual(ChronoLocalDate other)
- boolean isLeapYear()
- 윤년인지 checks- int lengthOfMonth()
- 해당 날짜의 월이 며칠까지 있는지- int lengthOfYear()
- 해당 연도의 일 수가 몇 개인지연산
연산을 수행한 복사본을 return
- LocalDate plusDays(long daysToAdd)
- LocalDate plusMonths(long monthsToAdd)
- LocalDate plusWeeks(long weeksToAdd)
- LocalDate plusYears(long yearsToAdd)
출력
- String toString()
- Outputs this date as a String, such as 2007-12-03
해당 문제는 매우 번거로웠던것으로 기억하니, 날짜를 다루는 실습만 해보자
import java.io.*;
import java.util.*;
import java.time.*;
public class Main {
static int T;
static StringBuilder sb = new StringBuilder();
static LocalDate dead, my;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = null;
T = Integer.parseInt(br.readLine());
for (int t=0; t<T; t++) {
st = new StringTokenizer(br.readLine());
String[] deadS = st.nextToken().split("/");
dead = LocalDate.of(Integer.parseInt(deadS[2]),Integer.parseInt(deadS[0]),Integer.parseInt(deadS[1]));
String[] myS = st.nextToken().split("/");
my = LocalDate.of(Integer.parseInt(deadS[2]),Integer.parseInt(myS[0]),Integer.parseInt(myS[1]));
int diff = Math.abs(dead.getDayOfYear() - my.getDayOfYear());
if (my.isEqual(dead)) sb.append("SAME DAY");
else if (diff > 7) sb.append("OUT OF RAGNE");
else {
sb.append(my.getMonthValue()).append("/").append(my.getDayOfMonth()).append("/").append(my.getYear());
sb.append(" IS ").append(diff).append(" DAY");
if (diff > 1) sb.append("S");
if (my.isAfter(dead)) sb.append(" AFTER");
else sb.append(" PRIOR");
}
sb.append("\n");
}
System.out.println(sb);
}
}


예제 입력을 넣으면 예제 출려과 유사하게 나온다.
맨 아래 두 줄은 이 문제에서 애매한 조건이 있었는데 그것을 처리 안해서 예제와 다르게 나왔다. 제출연도와 마감기한연도가 다를경우, 윤년인 경우 등 많은 골칫거리가 있었다.
LocalDate class 를 연습한 것으로 만족하자.