Java -- determine whether the date is before today
The date mentioned here refers to the date format of the string, for example, 2014-10-15. We need to determine whether the date is before today. There are many judgment methods on the Internet, convert the Date To the Date object and compare it with the new Date () method. Using the comparet To () or before () method will actually be a small problem, if the comparison is ignored for an hour, errors may occur and incorrect results may not be obtained.
If you want to get the correct result, convert the current time new Date () to the format of "yyyy-MM-dd", and then convert the two to date for determination. The Code is as follows:
Java. util. date nowdate = new java. util. date (); String date = DateUtils. formatDate (nowdate, "yyyy-MM-dd"); SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd", Locale. CHINA); Date d; try {d = sdf. parse (dateFormat); int flag = d. compareTo (DateUtils. formatDateFromeString (date); if (flag> = 0) {// catch (ParseException e) {// TODO Auto-generated catch blocke. printStackTrace ();}