Java Date Compare getCompareDate(String strDate1, String strDate2, String pFormat)

Here you can find the source of getCompareDate(String strDate1, String strDate2, String pFormat)

Description

get Compare Date

License

Open Source License

Declaration

public static int getCompareDate(String strDate1, String strDate2, String pFormat) throws Exception 

Method Source Code

//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;
    }
}

Related

  1. compareYear(java.util.Date date1, java.util.Date date2)
  2. compareYMD(Date begindate, Date enddate)
  3. compDate(Date date1, Date date2)
  4. compDate4(Date date1, Date date2)
  5. getCompareDate(Date startDate, Date endDate)
  6. getCompareDate(String strDate1, String strDate2, String pFormat)
  7. getCompareDateNum(String sDateValue1, String sDateValue2)
  8. getCompareResult(String date1, String date2)
  9. isAfterOrEqual(Date date, Date toCompare)