字符串日期比较

java.util.Date compareTo public int compareTo(Date anotherDate) 比较两个日期的顺序。 指定者: 接口 Comparable<date> 中的 compareTo 参数: anotherDate - 要比较的 Date。 返回: 如果参数 Date 等于此 Date,则返回值 0;如果此 Date 在 Date 参数之前,则返回小于 0 的值;如果此 Date 在 Date 参数之后,则返回大于 0 的值。 抛出: NullPointerException - 如果 anotherDate 为 null。 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); try { Date begin1=df.parse(cpbegindate); Date end1 =df.parse(enddate2); if(begin1.getTime()&gt;end1.getTime()) { messgelist.add("内部产品代码为"+scode+" 的产品的产品终止信息不正确,不能导入终止信息!"); break; } } catch (ParseException e1) { e1.printStackTrace(); } public static Date stringToDate(String date) { SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd"); Date dateb = null; try { dateb = bartDateFormat.parse(date); } catch (ParseException e1) { bartDateFormat = new SimpleDateFormat("yyyy/MM/dd"); try { dateb = bartDateFormat.parse(date); } catch (ParseException e2) { bartDateFormat = new SimpleDateFormat("yyyyMMdd"); try { dateb = bartDateFormat.parse(date); } catch (ParseException e3) { bartDateFormat = new SimpleDateFormat("yyyy-M-d"); try { dateb = bartDateFormat.parse(date); } catch (ParseException e4) { throw new RuntimeException("日期格式有误" + e4); } } } } return dateb; } public static boolean isRegularCRMS_DateFormat(String ddate) { return ddate.matches("\\d{4}-\\d{2}-\\d{2}"); } public static boolean before(String crmsDate1, String crmsDate2) { return stringToDate(crmsDate1).before(stringToDate(crmsDate2)); } public static boolean after(String crmsDate1, String crmsDate2) { return stringToDate(crmsDate1).after(stringToDate(crmsDate2)); }</date>

猜你喜欢

转载自ywwan2.iteye.com/blog/1059973