Here you can find the source of compareDate(String begingDate, String endDate, String format)
public static boolean compareDate(String begingDate, String endDate, String format)
//package com.java2s; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Main { public static boolean compareDate(String begingDate, String endDate) { boolean boo = true; Date begin = null;/* w w w .j a va2 s.c o m*/ Date end = null; String format = "yyyy/MM/dd"; // if(begingDate.indexOf("-") != -1) { // format = "yyyy-MM-dd"; // } // if(endDate.indexOf("-") != -1) { // format = "yyyy-MM-dd"; // } DateFormat df = new SimpleDateFormat(format); if (null == begingDate || "".equals(begingDate)) { boo = false; return boo; } if (null == endDate || "".equals(endDate)) { boo = false; return boo; } try { begin = df.parse(begingDate); end = df.parse(endDate); } catch (ParseException e) { e.printStackTrace(); } if (begin.compareTo(end) >= 0) { boo = false; } return boo; } public static boolean compareDate(String begingDate, String endDate, String format) { boolean boo = true; Date begin = null; Date end = null; if (format.isEmpty()) { format = format = "yyyy/MM/dd"; } // if(begingDate.indexOf("-") != -1) { // format = "yyyy-MM-dd"; // } // if(endDate.indexOf("-") != -1) { // format = "yyyy-MM-dd"; // } DateFormat df = new SimpleDateFormat(format); if (null == begingDate || "".equals(begingDate)) { boo = false; return boo; } if (null == endDate || "".equals(endDate)) { boo = false; return boo; } try { begin = df.parse(begingDate); end = df.parse(endDate); } catch (ParseException e) { e.printStackTrace(); } if (begin.compareTo(end) >= 0) { boo = false; } return boo; } public static String parse(Date d) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd"); String dateStr = sdf.format(d); return dateStr; } }