Here you can find the source of getCompareDate(String strDate1, String strDate2, String pFormat)
public static int getCompareDate(String strDate1, String strDate2, String pFormat) throws Exception
//package com.java2s; /**//from w w w .j ava2 s. c om * Copyright (C) 2012 Cardinal Info.Tech.Co.,Ltd. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License, version 3, * as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class Main { public static int getCompareDate(String strDate1, String strDate2, String pFormat) throws Exception { SimpleDateFormat fmt = new SimpleDateFormat(pFormat); Date date1 = fmt.parse(strDate1); Date date2 = fmt.parse(strDate2); return compareDate(date1, date2); } public static int compareDate(Date date1, Date date2) { Calendar c1 = Calendar.getInstance(); c1.setTime(date1); Calendar c2 = Calendar.getInstance(); c2.setTime(date2); return compareDate(c1, c2); } public static int compareDate(Calendar cal1, Calendar cal2) { int value = 9; if (cal1.before(cal2)) { value = -1; } if (cal1.after(cal2)) { value = 1; } if (cal1.equals(cal2)) { value = 0; } return value; } }