Here you can find the source of monthDiff(String beforeTime, String endTime)
public static int monthDiff(String beforeTime, String endTime)
//package com.java2s; public class Main { public static int monthDiff(String beforeTime, String endTime) { if (beforeTime == null || endTime == null) { return 0; }/* w w w .j a va2 s . co m*/ int beforeYear, endYear, beforeMonth, endMonth; try { beforeYear = Integer.parseInt(beforeTime.substring(0, 4)); endYear = Integer.parseInt(endTime.substring(0, 4)); beforeMonth = Integer.parseInt(beforeTime.substring(5, 7)) - 1; endMonth = Integer.parseInt(endTime.substring(5, 7)) - 1; return (endYear - beforeYear) * 12 + (endMonth - beforeMonth); } catch (Exception e) { e.printStackTrace(); } return 0; } }