Here you can find the source of compareDate(String strDate1, String strDate2, String format)
public static boolean compareDate(String strDate1, String strDate2, String format)
//package com.java2s; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class Main { public static boolean compareDate(String strDate1, String strDate2, String format) { Date date1 = convertStrToDate(strDate1, format); Date date2 = convertStrToDate(strDate2, format); if (date1.getTime() >= date2.getTime()) return true; return false; }// w w w. java 2 s. com /** * * * @param date1 * @param date2 * @param format * @return */ public static int compareDate(Date date1, Date date2, String format) { String dateStr1 = convertDateToStr(date1, format); String dateStr2 = convertDateToStr(date2, format); return dateStr1.compareTo(dateStr2); } public static int compareDate(Date date, Date startDate, Date endDate) { if (date == null || startDate == null || endDate == null) return -2; if (date.getTime() >= startDate.getTime() && date.getTime() <= endDate.getTime()) return 0; else if (date.getTime() > endDate.getTime()) return 1; else return -1; } public static Date convertStrToDate(String s) { try { DateFormat dateformat = DateFormat.getDateInstance(); Date date = dateformat.parse(s); return date; } catch (Exception exception) { exception.printStackTrace(); Calendar cal = Calendar.getInstance(); cal.set(1900, 0, 1); return cal.getTime(); } } public static Date convertStrToDate(String s, String format) { SimpleDateFormat simpledateformat = new SimpleDateFormat(format); try { Date date = simpledateformat.parse(s); return date; } catch (Exception exception) { exception.printStackTrace(); Calendar cal = Calendar.getInstance(); cal.set(1900, 0, 1); return cal.getTime(); } } public static String convertDateToStr(Date d, String format) { SimpleDateFormat simpledateformat = new SimpleDateFormat(format); String s; try { s = simpledateformat.format(d).toString(); return s; } catch (Exception e) { s = "1900-01-01"; } return s; } }