Java Date to Month getMonthOffset(Date sourceDate, Date targetDate)

Here you can find the source of getMonthOffset(Date sourceDate, Date targetDate)

Description

get Month Offset

License

Apache License

Declaration

public static int getMonthOffset(Date sourceDate, Date targetDate) 

Method Source Code

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

import java.util.*;

public class Main {

    public static int getMonthOffset(Date sourceDate, Date targetDate) {

        int sourceMonth = getMonth(sourceDate);
        int sourceYear = getYear(sourceDate);

        int targetMonth = getMonth(targetDate);
        int targetYear = getYear(targetDate);

        return (sourceYear - targetYear) * 12 + sourceMonth - targetMonth;

    }/*from ww  w. j  a  v  a 2 s  .  com*/

    public static int getMonth(Date date) {
        java.util.Calendar c = java.util.Calendar.getInstance();
        c.setTime(date);
        return c.get(java.util.Calendar.MONTH) + 1;
    }

    public static int getYear(Date date) {
        java.util.Calendar c = java.util.Calendar.getInstance();
        c.setTime(date);
        return c.get(java.util.Calendar.YEAR);
    }
}

Related

  1. getMonthLastDate(Date date)
  2. getMonthLastDay(Date date)
  3. getMonthlyKey(Date d)
  4. getMonthName(Date date)
  5. getMonthOfCurrentTime(Date date)
  6. getMonthOfThisYear(Date currentdate)
  7. getMonthOfYear(Date date)
  8. getMonthOfYear(Date dt)
  9. getMonthOfYear(final Date date)