一、LocalDate 的 isBefore()/isAfter()
返回值为 boolean 类型。
public static void main(String[] args) {
LocalDate ld = LocalDate.now();
LocalDate luckDate = LocalDate.of(2008, 8, 8);
System.out.println(ld.isBefore(luckDate));//false
System.out.println(ld.isAfter(luckDate));//true
}
二、【判断两个日期是否相等】Java8 的 LocalDate 重载了equal方法
注意,如果比较的日期是字符型的,需要先解析成日期对象再作判断。
//判断两个日期是否相等
public void compareDate() {
LocalDate curDate = LocalDate.now();
LocalDate myDate = LocalDate.of(2008, 8, 8);
if (myDate.equals(curDate)) {
System.out.printf("curDate: %s and myDate: %s are same date %n", curDate, myDate);
} else {
System.out.printf("curDate: %s and myDate: %s aren't same date %n", curDate, myDate);
}
}
//输出如下:
curDate: 2020-05-23 and myDate: 2008-08-08 aren't same date
三、Date 的 compareTo() 比较
因为 Date 实现了Comparable 接口,所以可以直接调用 Date 的 compareTo() 比较。
public class DateCompare {
public static void main(String[] args) {
String startTime = "2008-08-08 08:08:08";
String endTime = "2011-09-01 08:08:08";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date st = format.parse(startTime);
Date et = format.parse(endTime);
int compareTo = st.compareTo(et);
System.out.println(compareTo);//-1
} catch (ParseException e) {
e.printStackTrace();
}
}
}
compareTo() 的返回值,st 小于 et 返回 -1,st 大于 et 返回 1,相等返回 0。
四、Date 自带的 before()/after() 比较
返回值为 boolean 类型。
public class DateCompare {
public static void main(String[] args) {
String startTime = "2008-08-08 08:08:08";
String endTime = "2011-09-01 08:08:08";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date st = format.parse(startTime);
Date et = format.parse(endTime);
boolean before = st.before(et);
System.out.println(before);//true
System.out.println(st.after(et));//false
} catch (ParseException e) {
e.printStackTrace();
}
}
}
五、Date 的 getTime() 获取到毫秒数来进行比较
public class DateCompare {
public static void main(String[] args) {
String startTime = "2008-08-08 08:08:08";
String endTime = "2011-09-01 08:08:08";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date st = format.parse(startTime);
Date et = format.parse(endTime);
long stMillisecond = st.getTime();
long etMillisecond = et.getTime();
System.out.println(stMillisecond > etMillisecond);//false
} catch (ParseException e) {
e.printStackTrace();
}
}
}
六、LocalDateTime 计算两个日期间距离 Duration
public class DateCompare {
public static void main(String[] args) {
LocalDateTime startTime = LocalDateTime.of(2008, 8, 8, 0, 0, 0);
LocalDateTime endTime = LocalDateTime.of(2009, 8, 8, 0, 0, 0);
Duration duration = Duration.between(startTime, endTime);
long days = duration.toDays(); //相差的天数
long hours = duration.toHours();//相差的小时数
long minutes = duration.toMinutes();//相差的分钟数
long millis = duration.toMillis();//相差毫秒数
long nanos = duration.toNanos();//相差的纳秒数
System.out.println("startTime:" + startTime);
System.out.println("endTime:" + endTime);
System.out.println("相差时间【 " + days + "天:" + hours + " 小时:" +
minutes + " 分钟:" + millis + " 毫秒:" + nanos + " 纳秒】");
}
}
七、LocalDate 计算两个日期间距离 until()
public class DateCompare {
public static void main(String[] args) {
//计算两个日期间隔了【几年几个月零几天】
LocalDate startTime = LocalDate.parse("2008-08-08");
LocalDate endTime = LocalDate.parse("2018-09-01");
int years = startTime.until(endTime).getYears();
int months = startTime.until(endTime).getMonths();
int days = startTime.until(endTime).getDays();
System.out.println("间隔:" + years + "years," + months + "months and " + days + "days");
//计算两个日期间间隔多少年或者多少月或者多少天
LocalDate start = LocalDate.parse("2008-08-08");
LocalDate end = LocalDate.parse("2018-09-01");
long year = start.until(end, ChronoUnit.YEARS);
long month = start.until(end, ChronoUnit.MONTHS);
long day = start.until(end, ChronoUnit.DAYS);
System.out.println("间隔:" + year + "年");
System.out.println("间隔:" + month + "月");
System.out.println("间隔:" + day + "天");
}
}