๋ฐ์ํ
1. java.util.Date์๋ ๋ ์ง๋ฅผ ๋น๊ตํ ์ ์๋ ๋ฉ์๋๊ฐ ๋๊ฐ์ง ์๋ค.
- after, before ์ฌ์ฉ
- before: ์ธ์๋ณด๋ค ๊ณผ๊ฑฐ์ผ ๋ true
- after: ์ธ์๋ณด๋ค ๋ฏธ๋์ผ ๋ true
- equal: ์ธ์์ ๊ฐ์ ๋ true
public void compareDate(Date date1, Date date2){
if(date1.after(date2)){
System.out.println("Date1 > Date2");
}
if(date1.before(date2)){
System.out.println("Date1 < Date2");
}
if(date1.equals(date2)){
System.out.println("Date1 = Date2");
}
}
- compareTo ์ฌ์ฉ
- compareTo > 0 : ์ธ์๋ณด๋ค ๋ฏธ๋์ผ ๋
- compareTo < 0 : ์ธ์๋ณด๋ค ๊ณผ๊ฑฐ์ผ ๋
- compareTo = 0 : ์ธ์์ ๊ฐ์ ๋
public void compareDate2(Date date1, Date date2){
if (date1.compareTo(date2) > 0) {
System.out.println("Date1 > Date2");
}
if(date1.compareTo(date2) < 0){
System.out.println("Date1 < Date2");
}
if(date1.compareTo(date2) == 0){
System.out.println("Date1 = Date2");
}
}
2. java.time.LocalDateTime, LocalDate, LocalTime, ZonedDateTime (java 1.8๋ถํฐ ์ฌ์ฉ ๊ฐ๋ฅ)
- isBefore : ์ธ์๋ณด๋ค ๊ณผ๊ฑฐ์ผ ๋ true
- isAfter : ์ธ์๋ณด๋ค ๋ฏธ๋์ผ ๋ true
- isEqual : ์ธ์์ ๊ฐ์ ๋ true
public void compareDate3(LocalDateTime date1, LocalDateTime date2) {
if (date1.isBefore(date2)) {
System.out.println("Date1 < Date2");
}
if (date1.isAfter(date2)) {
System.out.println("Date1 > Date2");
}
if (date1.isEqual(date2)) {
System.out.println("Date1 = Date2");
}
}
728x90
๋ฐ์ํ
๋๊ธ