1.根據Date日期類比較
先看代碼,再來說明
public static void main(String[] args) throws InterruptedException { long begin = System.currentTimeMillis(); System.out.println("開始時間毫秒值:" + begin); //開始時間毫秒值:1591152816382 Date beginDate = new Date(); beginDate.setTime(begin); System.out.println("beginDate:" + beginDate); //beginDate:Wed Jun 03 10:53:36 CST 2020 Thread.sleep(2000); long end = System.currentTimeMillis(); System.out.println("結束時間毫秒值:" + end); //結束時間毫秒值:1591152818444 Date endDate = new Date(); endDate.setTime(end); System.out.println("endDate:" + endDate); //endDate:Wed Jun 03 10:53:38 CST 2020 if (beginDate.before(endDate)) { long difValue = (endDate.getTime() - beginDate.getTime()) / 1000; System.out.println(beginDate + "比" + endDate + "早" + difValue + "秒"); //Wed Jun 03 10:53:36 CST 2020比Wed Jun 03 10:53:38 CST 2020早2秒 } if (beginDate.after(endDate)) { long difValue = (beginDate.getTime() - endDate.getTime()) / 1000; System.out.println(beginDate + "比" + endDate + "晚" + difValue + "秒"); } int difValue = beginDate.compareTo(endDate); if (difValue == -1) { System.out.println(beginDate + "在" + endDate + "之前"); //Wed Jun 03 10:53:36 CST 2020在Wed Jun 03 10:53:38 CST 2020之前 } else if (difValue == 0) { System.out.println("時間相同"); } else if (difValue == 1) { System.out.println(beginDate + "在" + endDate + "之后"); } }
對應Date類型,我們可以通過其自帶的API進行時間比較,主要有三種
1.1 before() 判斷前邊的時間是否在后邊的時間之前,返回boolean值
public boolean before(Date when) { return getMillisOf(this) < getMillisOf(when); }
1.2 after() 判斷前邊的時間是否在后邊的時間之后,返回boolean值
public boolean after(Date when) { return getMillisOf(this) > getMillisOf(when); }
1.3compareTo() 判斷前后時間的大小,返回int值
例如1.compareTo(2) 如果1在2之前,返回-1;如果1在2之后,返回1;如果1和2時間相同,返回0
public int compareTo(Date anotherDate) { long thisTime = getMillisOf(this); long anotherTime = getMillisOf(anotherDate); return (thisTime<anotherTime ? -1 : (thisTime==anotherTime ? 0 : 1)); }
2. 根據Calendar日歷類比較
Calendar類可以看做是Date類的加強,比較方法類似,代碼即可說明
public static void main(String[] args) throws InterruptedException { Calendar calendar = Calendar.getInstance(); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH) + 1;//默認從0開始 int day = calendar.get(Calendar.DAY_OF_MONTH); int hour = calendar.get(Calendar.HOUR_OF_DAY); int minute = calendar.get(Calendar.MINUTE); int second = calendar.get(Calendar.SECOND); String currentTime = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second; System.out.println("系統當前時間:" + currentTime); //系統當前時間:2020-6-3 14:15:38 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { Date currentDate = simpleDateFormat.parse(currentTime); System.out.println("系統當前時間" + currentDate); //系統當前時間Wed Jun 03 14:15:38 CST 2020 Calendar calendar1 = Calendar.getInstance(); calendar1.setTime(currentDate); Calendar calendar2 = Calendar.getInstance(); calendar2.add(Calendar.DAY_OF_MONTH, 1); System.out.println("當前時間加上1天后的時間:" + calendar2.getTime()); //當前時間加上1天后的時間:Thu Jun 04 14:15:38 CST 2020 long difValue = 0; if (calendar1.before(calendar2)) { difValue = (calendar2.getTime().getTime() - calendar1.getTime().getTime()) / 3600000; System.out.println(calendar1.getTime() + "比" + calendar2.getTime() + "早" + difValue + "小時"); //Wed Jun 03 14:15:38 CST 2020比Thu Jun 04 14:15:38 CST 2020早24小時 } int value = calendar1.compareTo(calendar2); if (value == -1) { difValue = (calendar2.getTime().getTime() - calendar1.getTime().getTime()) / 3600000; System.out.println(calendar1.getTime() + "比" + calendar2.getTime() + "早" + difValue + "小時"); //Wed Jun 03 14:15:38 CST 2020比Thu Jun 04 14:15:38 CST 2020早24小時 } else if (value == 1) { System.out.println(calendar1.getTime() + "比" + calendar2.getTime() + "晚"); } else if (value == 0) { System.out.println("時間一致"); } } catch (ParseException e) { e.printStackTrace(); } }