문제
내가 작성한 정답
if - else
class Solution {
public int solution(int[] date1, int[] date2) {
if(date1[0]!=date2[0]) return date1[0]<date2[0]?1:0;
else{
if(date1[1]!=date2[1]) return date1[1]<date2[1]?1:0;
else{
return date1[2]<date2[2]?1:0;
}
}
}
}
for
class Solution {
public int solution(int[] date1, int[] date2) {
for(int i=0; i<3;i++){
if(date1[i]!=date2[i]) return date1[i]<date2[i]?1:0;
}
return 0;
}
}
stream

import java.util.stream.*;
class Solution {
public int solution(int[] date1, int[] date2) {
return IntStream.range(0,3)
.filter(i->date1[i]!=date2[i])
.boxed()
.findFirst()
.map(i->date1[i]<date2[i]?1:0)
.orElse(0);
}
}
다른 사람들의 정답
LocalDate
import java.time.LocalDate;
class Solution {
public int solution(int[] date1, int[] date2) {
LocalDate dateA = LocalDate.of(date1[0], date1[1], date1[2]);
LocalDate dateB = LocalDate.of(date2[0], date2[1], date2[2]);
if (dateA.isBefore(dateB)) {
return 1;
} else {
return 0;
}
}
}
LocalDate
: 연도, 월, 일로 구성된 날짜를 나타내며 시간 정보는 포함하지 않는다
now(): 현재 날짜를 가져온다.
of(int year, int month, int dayOfMonth): 지정된 연도, 월, 일로 LocalDate 객체를 생성한다.
getYear(), getMonthValue(), getDayOfMonth(): 연도, 월, 일을 가져온다
isLeapYear(): 윤년인지 확인한다
plusDays(long daysToAdd), minusWeeks(long weeksToSubtract): 날짜를 더하거나 뺀다
isBefore(ChronoLocalDate other), isAfter(ChronoLocalDate other): 다른 날짜와 비교한다
format(DateTimeFormatter formatter): 지정된 형식으로 날짜를 문자열로 변환한다
LocalTime
: 시, 분, 초, 나노초로 구성된 시간을 나타내며 날짜 정보는 포함하지 않는다
now(): 현재 시간을 가져온다
of(int hour, int minute, int second, int nanoOfSecond): 지정된 시, 분, 초, 나노초로 LocalTime 객체를 생성한다
getHour(), getMinute(), getSecond(), getNano(): 시, 분, 초, 나노초를 가져온다
plusHours(long hoursToAdd), minusMinutes(long minutesToSubtract): 시간을 더하거나 뺀다
isBefore(LocalTime other), isAfter(LocalTime other): 다른 시간과 비교한다
format(DateTimeFormatter formatter): 지정된 형식으로 시간을 문자열로 변환한다
LocalDateTime
: LocalDate와 LocalTime을 결합한 것으로, 날짜와 시간을 모두 나타낸다.
now(): 현재 날짜와 시간을 가져온다
of(int year, int month, int dayOfMonth, int hour, int minute, int second): 지정된 연도, 월, 일, 시, 분, 초로 LocalDateTime 객체를 생성한다
getYear(), getMonthValue(), getDayOfMonth(), getHour(), getMinute(), getSecond(): 연도, 월, 일, 시, 분, 초를 가져온다
toLocalDate(), toLocalTime(): LocalDate와 LocalTime 객체를 추출한다
plusYears(long yearsToAdd), minusMonths(long monthsToSubtract): 연도나 월을 더하거나 뺀다
isBefore(ChronoLocalDateTime other), isAfter(ChronoLocalDateTime other): 다른 날짜 및 시간과 비교한다
format(DateTimeFormatter formatter): 지정된 형식으로 날짜와 시간을 문자열로 변환한다
Arrays.compare
import java.util.*;
class Solution {
public int solution(int[] date1, int[] date2) {
return Arrays.compare(date1, date2) < 0 ? 1 : 0;
}
}
일수로 변환하여 결과 도출
class Solution {
public int solution(int[] date1, int[] date2) {
// getTotalDays() 메서드를 호출하여 각 날짜를 총 일수 변환
int date1Days = getTotalDays(date1);
int date2Days = getTotalDays(date2);
return date1Days < date2Days ? 1 : 0;
}
public int getTotalDays(int[] date) {
int result = 0;
result += date[0] * 360; // 년
result += date[1] * 30; // 월
result += date[2]; // 일
return result;
}
}
배열을 연결하여 숫자로 변환하여 비교
class Solution {
public int solution(int[] date1, int[] date2) {
return Integer.parseInt(date1[0] + "" + date1[1] + "" + date1[2]) >= Integer.parseInt(date2[0] + "" + date2[1] + "" + date2[2]) ? 0 : 1;
}
}
Share article