Java Date Difference dateDiffMonth(Date d1, Date d2)

Here you can find the source of dateDiffMonth(Date d1, Date d2)

Description

date Diff Month

License

Apache License

Declaration

public static long dateDiffMonth(Date d1, Date d2) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

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

public class Main {

    public static long dateDiffMonth(Date d1, Date d2) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d1);/*from  w  w w .  j  av  a 2  s. c  o m*/
        int y1 = calendar.get(Calendar.YEAR);
        int m1 = calendar.get(Calendar.MONTH);
        calendar.setTime(d2);
        int y2 = calendar.get(Calendar.YEAR);
        int m2 = calendar.get(Calendar.MONTH);

        return (y1 - y2) * 12 + m1 - m2;
    }

    public static int get(Date date, int field) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(field);
    }
}

Related

  1. dateDiff(String startTime, String endTime, String format)
  2. DateDiff(String strDateBegin, String strDateEnd, int iType)
  3. dateDiffer(String time1, String time2, String formatStr)
  4. dateDiffInDays(Date dateStart, Date dateEnd)
  5. dateDiffInDaysIgnoreTime(Date dateStart, Date dateEnd)
  6. dateDiffWithNow(Calendar sDate)
  7. dayDiff(Date a, Date b)
  8. dayDiff(Date date1, Date date2)
  9. dayDiff(Date first, Date second)