[Java] String 날짜, 시간 비교
문자열(String)로 표현된 날짜, 시간들을 비교하는 방법을 알아보겠습니다. 날짜/시간을 비교하면 어떤 시간이 빠른지, 느린지 알 수 있습니다.
1. 날짜, 시간 문자열 비교
SimpleDateFormat과 Date를 이용한 방법
아래와 같이 SimpleDateFormat과 Date를 이용하여 문자열로 된 날짜, 시간 문자열을 파싱할 수 있습니다.
파싱된 Date 객체는 before()
와 after()
함수를 이용하여 시간 선, 후 관계를 비교할 수 있습니다.
A.before(B)
: A가 B보다 이전 시간일 때 true 리턴A.after(B)
: A가 B보다 이후 시간일 때 true 리턴- before()와 after()가 모두 false일 때는 같은 시간
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Example {
public static void main(String[] args) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString1 = "2023-08-25 10:30:00";
String dateString2 = "2023-08-25 12:45:00";
try {
Date date1 = dateFormat.parse(dateString1);
Date date2 = dateFormat.parse(dateString2);
if (date1.before(date2)) {
System.out.println(dateString1 + " is before " + dateString2);
} else if (date1.after(date2)) {
System.out.println(dateString1 + " is after " + dateString2);
} else {
System.out.println(dateString1 + " is equal to " + dateString2);
}
} catch (ParseException e) {
e.printStackTrace();
}
}
}
Output:
2023-08-25 10:30:00 is before 2023-08-25 12:45:00
LocalDateTime을 이용한 방법
SimpleDateFormat.parse()
대신에 LocalDateTime.parse()
로 날짜, 시간 문자열을 파싱하여 LocalDateTime 객체로 만들 수 있습니다. 대신, 인자로 문자열과 SimpleDateFormat 객체를 전달합니다.
파싱된 LocalDateTime 객체는 isBefore()
와 isAfter()
함수를 이용하여 시간 선, 후 관계를 비교할 수 있습니다.
A.isBefore(B)
: A가 B보다 이전 시간일 때 true 리턴A.isAfter(B)
: A가 B보다 이후 시간일 때 true 리턴- isBefore()와 isAfter()가 모두 false일 때는 같은 시간
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String dateString1 = "2023-08-25 10:30:00";
String dateString2 = "2023-08-25 12:45:00";
LocalDateTime dateTime1 = LocalDateTime.parse(dateString1, formatter);
LocalDateTime dateTime2 = LocalDateTime.parse(dateString2, formatter);
if (dateTime1.isBefore(dateTime2)) {
System.out.println(dateString1 + " is before " + dateString2);
} else if (dateTime1.isAfter(dateTime2)) {
System.out.println(dateString1 + " is after " + dateString2);
} else {
System.out.println(dateString1 + " is equal to " + dateString2);
}
2. 시간 문자열 비교
SimpleDateFormat과 Date를 이용한 방법
아래와 같이 SimpleDateFormat과 Date를 이용하여 문자열로 된 시간 문자열을 파싱할 수 있습니다.
파싱된 Date 객체는 before()
와 after()
함수를 이용하여 시간 선, 후 관계를 비교할 수 있습니다.
A.before(B)
: A가 B보다 이전 시간일 때 true 리턴A.after(B)
: A가 B보다 이후 시간일 때 true 리턴- before()와 after()가 모두 false일 때는 같은 시간
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Example {
public static void main(String[] args) {
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
String timeString1 = "10:30:00";
String timeString2 = "12:45:00";
try {
Date time1 = timeFormat.parse(timeString1);
Date time2 = timeFormat.parse(timeString2);
if (time1.before(time2)) {
System.out.println(timeString1 + " is before " + timeString2);
} else if (time1.after(time2)) {
System.out.println(timeString1 + " is after " + timeString2);
} else {
System.out.println(timeString1 + " is equal to " + timeString2);
}
} catch (ParseException e) {
e.printStackTrace();
}
}
}
Output:
10:30:00 is before 12:45:00
LocalTime을 이용한 방법
SimpleDateFormat.parse()
대신에 LocalTime.parse()
로 시간 문자열을 파싱하여 LocalTime 객체로 만들 수 있습니다. 대신, 인자로 문자열과 DateTimeFormatter 객체를 전달합니다.
파싱된 LocalTime 객체는 isBefore()
와 isAfter()
함수를 이용하여 시간 선, 후 관계를 비교할 수 있습니다.
A.isBefore(B)
: A가 B보다 이전 시간일 때 true 리턴A.isAfter(B)
: A가 B보다 이후 시간일 때 true 리턴- isBefore()와 isAfter()가 모두 false일 때는 같은 시간
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
String timeString1 = "10:30:00";
String timeString2 = "12:45:00";
LocalTime time1 = LocalTime.parse(timeString1, formatter);
LocalTime time2 = LocalTime.parse(timeString2, formatter);
if (time1.isBefore(time2)) {
System.out.println(timeString1 + " is before " + timeString2);
} else if (time1.isAfter(time2)) {
System.out.println(timeString1 + " is after " + timeString2);
} else {
System.out.println(timeString1 + " is equal to " + timeString2);
}