很少有示例向您展示如何在Java中比较两个日期。 更新了Java 8示例。
1. Date.compareTo()
在Java中比较两个java.util.Date
经典方法。
- 如果两个日期相等,则返回值为0。
- 如果Date在date参数之后,则返回值大于0。
- 如果Date在date参数之前,则返回值小于0。
TestDate.java
package com.mkyong.date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TestDate {
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = sdf.parse("2009-12-31");
Date date2 = sdf.parse("2010-01-31");
System.out.println("date1 : " + sdf.format(date1));
System.out.println("date2 : " + sdf.format(date2));
if (date1.compareTo(date2) > 0) {
System.out.println("Date1 is after Date2");
} else if (date1.compareTo(date2) < 0) {
System.out.println("Date1 is before Date2");
} else if (date1.compareTo(date2) == 0) {
System.out.println("Date1 is equal to Date2");
} else {
System.out.println("How to get here?");
}
}
}
输出量
date1 : 2009-12-31
date2 : 2010-01-31
Date1 is before Date2
2. Date.before(),Date.after()和Date.equals()
比较两个java.util.Date
一种更加用户友好的方法
TestDate2.java
package com.mkyong.date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TestDate2 {
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = sdf.parse("2009-12-31");
Date date2 = sdf.parse("2010-01-31");
System.out.println("date1 : " + sdf.format(date1));
System.out.println("date2 : " + sdf.format(date2));
if (date1.after(date2)) {
System.out.println("Date1 is after Date2");
}
if (date1.before(date2)) {
System.out.println("Date1 is before Date2");
}
if (date1.equals(date2)) {
System.out.println("Date1 is equal Date2");
}
}
}
输出量
date1 : 2009-12-31
date2 : 2010-01-31
Date1 is before Date2
3. Calender.before(),Calender.after()和Calender.equals()
比较两个java.util.Calendar
示例
TestDate3.java
package com.mkyong.date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class TestDate3 {
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = sdf.parse("2009-12-31");
Date date2 = sdf.parse("2010-01-31");
System.out.println("date1 : " + sdf.format(date1));
System.out.println("date2 : " + sdf.format(date2));
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
cal1.setTime(date1);
cal2.setTime(date2);
if (cal1.after(cal2)) {
System.out.println("Date1 is after Date2");
}
if (cal1.before(cal2)) {
System.out.println("Date1 is before Date2");
}
if (cal1.equals(cal2)) {
System.out.println("Date1 is equal Date2");
}
}
}
输出量
date1 : 2009-12-31
date2 : 2010-01-31
Date1 is before Date2
4. Java 8
在Java 8中,可以使用新的isBefore(),isAfter(),isEqual()和compareTo()来比较LocalDate,LocalTime和LocalDateTime。
查看下面的示例以比较两个java.time.LocalDate
TestDate4.java
package com.mkyong.date;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class TestDate4 {
public static void main(String[] args) {
DateTimeFormatter sdf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate date1 = LocalDate.of(2009, 12, 31);
LocalDate date2 = LocalDate.of(2010, 01, 31);
System.out.println("date1 : " + sdf.format(date1));
System.out.println("date2 : " + sdf.format(date2));
System.out.println("Is...");
if (date1.isAfter(date2)) {
System.out.println("Date1 is after Date2");
}
if (date1.isBefore(date2)) {
System.out.println("Date1 is before Date2");
}
if (date1.isEqual(date2)) {
System.out.println("Date1 is equal Date2");
}
System.out.println("CompareTo...");
if (date1.compareTo(date2) > 0) {
System.out.println("Date1 is after Date2");
} else if (date1.compareTo(date2) < 0) {
System.out.println("Date1 is before Date2");
} else if (date1.compareTo(date2) == 0) {
System.out.println("Date1 is equal to Date2");
} else {
System.out.println("How to get here?");
}
}
}
输出量
date1 : 2009-12-31
date2 : 2010-01-31
Is...
Date1 is before Date2
CompareTo...
Date1 is before Date2