先转成date类型。再比较,比如:
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class date { public static void main(String[] args) throws ParseException{ int compareTo=0; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String nowDate_String = sdf.format( new Date() ); String endDate_String = "2019-1-1"; Date nowDate_Date = sdf.parse(nowDate_String); Date endDate_Date = sdf.parse(endDate_String); compareTo = nowDate_Date.compareTo(endDate_Date); if(compareTo == -1){ System.out.println(nowDate_String+"比"+endDate_String+"早"); }else{ System.out.println(nowDate_String+"比"+endDate_String+"晚"); } } }