Created
April 17, 2019 09:47
-
-
Save yayyz/7171d7927d4e936951c261aadd0a35bd to your computer and use it in GitHub Desktop.
그냥 leftDate.compareTo(rightDate) 하면 되지만, 내부는 이런식으로 되어있음.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 날짜 비교 compareTo | |
*/ | |
public static int compareTo(LocalDateTime leftDate, LocalDateTime rightDate) { | |
int cmp = compareDate(leftDate, rightDate); //compare date | |
if (cmp == 0) { | |
// compare time | |
cmp = compareTime(leftDate.toLocalTime(), rightDate.toLocalTime()); | |
} | |
return cmp; | |
} | |
// LocalDate의 compareTo0 | |
private static int compareDate(LocalDateTime leftDate, LocalDateTime rightDate) { | |
int cmp = leftDate.getYear() - rightDate.getYear(); | |
if (cmp == 0) { | |
cmp = leftDate.getMonthValue() - rightDate.getMonthValue(); | |
if (cmp == 0) { | |
cmp = leftDate.getDayOfMonth() - rightDate.getDayOfMonth(); | |
} | |
} | |
return cmp; | |
} | |
// LocalTime의 compareTo | |
private static int compareTime(LocalTime leftTime, LocalTime rightTime) { | |
int cmp = Integer.compare(leftTime.getHour(), rightTime.getHour()); | |
if (cmp == 0) { | |
cmp = Integer.compare(leftTime.getMinute(), rightTime.getMinute()); | |
if (cmp == 0) { | |
cmp = Integer.compare(leftTime.getSecond(), rightTime.getSecond()); | |
if (cmp == 0) { | |
cmp = Integer.compare(leftTime.getNano(), rightTime.getNano()); | |
} | |
} | |
} | |
return cmp; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment