Home >Java >javaTutorial >How to determine whether dates are equal in java
You can use the equals() method in the Date class to compare.
Syntax:
boolean equals(Object date)
Returns true when the Date object calling this method is equal to the specified date, otherwise returns false.
(Recommended video tutorial: java video)
Specific code:
//1.利用Date的equals比较大小 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String timeStr1 = "2020-02-12 12:45:45"; String timeStr2 = "2020-02-12 12:45:40"; Date date1 = sdf.parse(timeStr1); Date date2 = sdf.parse(timeStr2); if(date1.equals(date2)) { //相等 }
Recommended tutorial: Getting started with java development
The above is the detailed content of How to determine whether dates are equal in java. For more information, please follow other related articles on the PHP Chinese website!