Java Month getMonthBetween(String strDate, int intDiff)

Here you can find the source of getMonthBetween(String strDate, int intDiff)

Description

get Month Between

License

Open Source License

Declaration

public static String getMonthBetween(String strDate, int intDiff) 

Method Source Code

//package com.java2s;

public class Main {

    public static String getMonthBetween(String strDate, int intDiff) {
        try {//from w w  w.  ja  v  a 2 s  . com
            int intYear = Integer.parseInt(strDate.substring(0, 4));
            int intMonth = Integer.parseInt(strDate.substring(4, 6));
            String strDay = "";
            if (strDate.length() > 6)
                strDay = strDate.substring(6, strDate.length());
            for (intMonth += intDiff; intMonth <= 0; intMonth += 12)
                intYear--;
            for (; intMonth > 12; intMonth -= 12)
                intYear++;
            if (intMonth < 10) {
                return Integer.toString(intYear) + "-0" + Integer.toString(intMonth) + strDay;
            }
            return Integer.toString(intYear) + "-" + Integer.toString(intMonth) + strDay;
        } catch (Exception e) {
            return "";
        }
    }

    public static String getMonthBetween(String strDateBegin, String strDateEnd) {
        try {
            String strOut;
            if (strDateBegin.equals("") || strDateEnd.equals("") || strDateBegin.length() != 6
                    || strDateEnd.length() != 6) {
                strOut = "";
            } else {
                int intMonthBegin = Integer.parseInt(strDateBegin.substring(0, 4)) * 12
                        + Integer.parseInt(strDateBegin.substring(4, 6));
                int intMonthEnd = Integer.parseInt(strDateEnd.substring(0, 4)) * 12
                        + Integer.parseInt(strDateEnd.substring(4, 6));
                strOut = String.valueOf(intMonthBegin - intMonthEnd);
            }
            return strOut;
        } catch (Exception e) {
            return "0";
        }
    }
}

Related

  1. getFirstMonthOfQuarter(String quarter)
  2. getFiscalMonth(int month, int fiscalMonthOffset)
  3. getFrontMonth(String month)
  4. getMonthBegin(String strdate)
  5. getMonthBeginByMonth(String month)
  6. getMonthByDate(String yyyyMMdd)
  7. getMonthCountByBillMode(String billMode)
  8. getMonthDescription(int month)
  9. getMonthDiff(String startDate, String endDate)