Here you can find the source of diffDateM(Date sd, Date ed)
Parameter | Description |
---|---|
sd | the start date |
ed | the end date |
Parameter | Description |
---|---|
ParseException | an exception |
static public int diffDateM(Date sd, Date ed) throws ParseException
//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; } }