Here you can find the source of monthsDiff(final Date data1, final Date data2)
Parameter | Description |
---|---|
data1 | The first date |
data2 | The second date |
public static int monthsDiff(final Date data1, final Date data2)
//package com.java2s; //License from project: Open Source License import java.util.Calendar; import java.util.Date; public class Main { /**//from w w w . j a v a 2 s .com * Get the difference in months of two given dates. * * @param data1 * The first date * @param data2 * The second date * @return The difference in months of two given dates */ public static int monthsDiff(final Date data1, final Date data2) { Calendar cal = Calendar.getInstance(); cal.setTime(data1); int monthA = cal.get(Calendar.MONTH); int yearA = cal.get(Calendar.YEAR); cal.setTime(data2); int monthB = cal.get(Calendar.MONTH); int yearB = cal.get(Calendar.YEAR); return ((yearA - yearB) * cal.getMaximum(Calendar.MONTH)) + (monthA - monthB); } }