Java Date Difference diffDateM(Date sd, Date ed)

Here you can find the source of diffDateM(Date sd, Date ed)

Description

Get the total month from two date.

License

Open Source License

Parameter

Parameter Description
sd the start date
ed the end date

Exception

Parameter Description
ParseException an exception

Return

int month form the start to end date

Declaration

static public int diffDateM(Date sd, Date ed) throws ParseException 

Method Source Code

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

import java.text.ParseException;

import java.util.Calendar;
import java.util.Date;

public class Main {
    /**//  ww w .  j  a v a2  s  .com
     * Get the total month from two date.
     * 
     * @param sd
     *            the start date
     * @param ed
     *            the end date
     * @return int month form the start to end date
     * @throws ParseException
     */
    static public int diffDateM(Date sd, Date ed) throws ParseException {
        Calendar c_sd = Calendar.getInstance();
        Calendar c_ed = Calendar.getInstance();
        c_sd.setTime(sd);
        c_ed.setTime(ed);
        return (c_ed.get(Calendar.YEAR) - c_sd.get(Calendar.YEAR)) * 12 + c_ed.get(Calendar.MONTH)
                - c_sd.get(Calendar.MONTH) + 1;
        /*
        return (ed.getYear() - sd.getYear()) * 12 + ed.getMonth()
        - sd.getMonth() + 1;
        */
    }

    static public int diffDateM(int sym, int eym) throws ParseException {
        return (Math.round((float) eym / 100) - Math.round((float) sym / 100)) * 12 + (eym % 100 - sym % 100) + 1;
    }
}

Related

  1. diffBetweenMonth(Date d1, Date d2)
  2. diffCommercial(Date dateUntil, Date dateFrom, boolean bAddDay)
  3. diffDate(Date date, Date date1)
  4. diffDate(java.util.Date date, java.util.Date date1)
  5. diffDateD(Date sd, Date ed)
  6. diffDates(long starttime, long endtime, int type)
  7. diffDateSec(java.util.Date date, java.util.Date date1)
  8. diffDay(Date date1, Date date2)
  9. diffDay(Date start, Date end)