Home  >  Article  >  Java  >  How to determine whether dates are equal in java

How to determine whether dates are equal in java

王林
王林Original
2020-05-18 15:37:406074browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn