Search code examples
javadategregorian-calendar

How to substract two dates in GregorianCallendar?


How to write a function which returns number of days between two dates in Gregorian Callendar? What type should I convert my dates and how?

public int substractDate(GregorianCalendar beginningDate, GregorianCalendar endingDate){
            return dif;
        }

Solution

  • Use GregorianCalendar object which is a subclass of Calendar:

        String dayPattern = "DD";
        SimpleDateFormat days = new SimpleDateFormat(dayPattern);
        return Integer.parseInt(days.format(endingDate.getTimeInMillis()-beginningDate.getTimeInMillis()));
    

    Oracle docs