java两个日期比较相等

1.利用Date的equals比较大小

//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)) {
	//相等
}

2.题外:利用Calendar的equals比较

Calendar time1 = Calendar.getInstance();
Calendar time2 = Calendar.getInstance();
time1.setTime(date1);
time2.setTime(date2);
if(time1.equals(time2)) {
    //相等
}

 

你可能感兴趣的:(java)