Java Date Compare compareDate(String strDate1, String strDate2, String format)

Here you can find the source of compareDate(String strDate1, String strDate2, String format)

Description

compare Date

License

Open Source License

Declaration

public static boolean compareDate(String strDate1, String strDate2, String format) 

Method Source Code


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

Related

  1. compareDate(String s, String e)
  2. compareDate(String sDate, String eDate, String formatStr)
  3. compareDate(String sFirstDate, String sSecondDate)
  4. compareDate(String startdate, String enddate)
  5. compareDate(String startDate, String endDate, String strFormat)
  6. compareDate2(String begingDate, String endDate, String format)
  7. compareDateFormat(String strDate1, String strDate2, String format)
  8. compareDateForSecond(Date src, Date target, int second)
  9. compareDates(Date date1, Date date2)