java比较两个日期的大小,与当前日期比较大小

public static void main(String[] args) {
//获取当前时间
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String dateString = formatter.format(currentTime);

String time="2019-07-01";

//比当前日期小返回-1 	相等返回 0  比当前日期大 返回1  
int a=timeCompare(time,dateString)
System.out.println(a);
}

/*时间比大小*/
public static int timeCompare(String t1,String t2){
	SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
	Calendar c1=Calendar.getInstance();
	Calendar c2=Calendar.getInstance();
	try {
		c1.setTime(formatter.parse(t1));
		c2.setTime(formatter.parse(t2));
	} catch (ParseException e) {
			e.printStackTrace();
	}
	int result=c1.compareTo(c2);
	return result;
	}

 

你可能感兴趣的:(java基础知识)