Here you can find the source of getMonthBetween(String strDate, int intDiff)
public static String getMonthBetween(String strDate, int intDiff)
//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"; } } }