Java Date Compare compareCalendars(Date fechaAComparar, Date fechaBase)

Here you can find the source of compareCalendars(Date fechaAComparar, Date fechaBase)

Description

compare Calendars

License

Open Source License

Declaration

public static int compareCalendars(Date fechaAComparar, Date fechaBase) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Calendar;
import java.text.DateFormat;

public class Main {

    public static int compareCalendars(Date fechaAComparar, Date fechaBase) {
        Calendar calendario1 = Calendar.getInstance();
        calendario1.setTime(fechaAComparar);
        Calendar calendario2 = Calendar.getInstance();
        calendario2.setTime(fechaBase);/*from ww  w . ja  v a2  s . com*/
        if (calendario1.get(Calendar.YEAR) == calendario2.get(Calendar.YEAR)
                && calendario1.get(Calendar.MONTH) == calendario2.get(Calendar.MONTH)
                && calendario1.get(Calendar.DATE) == calendario2.get(Calendar.DATE)) {
            return 0;
        }
        if (calendario1.get(Calendar.YEAR) >= calendario2.get(Calendar.YEAR)
                && calendario1.get(Calendar.MONTH) >= calendario2.get(Calendar.MONTH)
                && calendario1.get(Calendar.DATE) > calendario2.get(Calendar.DATE)) {
            return 1;
        }
        if (calendario1.get(Calendar.YEAR) <= calendario2.get(Calendar.YEAR)
                && calendario1.get(Calendar.MONTH) <= calendario2.get(Calendar.MONTH)
                && calendario1.get(Calendar.DATE) < calendario2.get(Calendar.DATE)) {
            return -1;
        }
        return 99;
    }

    public static int compareCalendars(String fechaAComparar, String fechaBase) {

        int valor_retorno = -1;

        DateFormat formato = new SimpleDateFormat("yyyy-MM-dd");

        try {
            Date d1 = formato.parse(fechaAComparar);
            Date d2 = formato.parse(fechaBase);

            if (d1.equals(d2))
                valor_retorno = 0;
            else if (d1.before(d2))
                valor_retorno = -1;
            else if (d1.after(d2))
                valor_retorno = 1;
            else
                valor_retorno = 99;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return valor_retorno;
    }
}

Related

  1. compare(final Date one, final Date another)
  2. compare2Date(Date date1, Date date2)
  3. compare2Dates(Date d1, Date d2)
  4. compare2DateString(String DateStringType, String dateStr1, String dateStr2)
  5. compare_date2(String finalDate, String enddate)
  6. compareDate(Date d1, Date d2)
  7. compareDate(Date d1, Date d2)
  8. compareDate(Date d1, Date d2)
  9. CompareDate(Date date, Date otherdate)