2个java.util.Date之间的月数,不包括日期

我想要两个java.util.Date之间的月数,而不计算月份的日期。 所以我只想比较年份和月份。

例:

  monthsBetween(new Date(2012,01,28), new Date(2012,02,01)) ---> 1 monthsBetween(new Date(2012,02,27), new Date(2012,02,28)) ---> 0 monthsBetween(new Date(2012,03,28), new Date(2012,07,01)) ---> 4 

我试过这个(返回0,预期1),使用Joda-time:

 private static int monthsBetween(final Date fromDate, final Date toDate) { DateTime date1 = new DateTime().withDate(2012, 1, 20); DateTime date2 = new DateTime().withDate(2012, 2, 13); PeriodType monthDay = PeriodType.yearDayTime().withDaysRemoved(); Period difference = new Period(date1, date2, monthDay); int months = difference.getMonths(); return months; } 

而且这个(相同的结果),使用Joda-time:

 private static int monthsBetween(final Date fromDate, final Date toDate) { return Months.monthsBetween(new DateTime(fromDate), new DateTime(toDate).getMonths(); } 

我怎么能做到这一点?

你要求整个月的数量 – 这与说“忽略月份部分”不同。

首先,我建议使用LocalDate而不是DateTime进行计算。 理想情况下,根本不要使用java.util.Date ,并将输入作为LocalDate开始(例如,通过直接解析文本,或者数据来自哪里。)在两个日期中将月中的日期设置为1 , 然后在几个月采取差异:

 private static int monthsBetweenIgnoreDays(LocalDate start, LocalDate end) { start = start.withDayOfMonth(1); end = end.withDayOfMonth(1); return Months.monthsBetween(start, end).getMonths(); } 

此版本基于JDK Calendar:

 public static void main(String[] args) throws Exception { SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd"); Date d1 = f.parse("2012-01-01"); Date d2 = f.parse("2012-02-02"); int n = differenceInMonths(d1, d2); System.out.println(n); } private static int differenceInMonths(Date d1, Date d2) { Calendar c1 = Calendar.getInstance(); c1.setTime(d1); Calendar c2 = Calendar.getInstance(); c2.setTime(d2); int diff = 0; if (c2.after(c1)) { while (c2.after(c1)) { c1.add(Calendar.MONTH, 1); if (c2.after(c1)) { diff++; } } } else if (c2.before(c1)) { while (c2.before(c1)) { c1.add(Calendar.MONTH, -1); if (c1.before(c2)) { diff--; } } } return diff; } 

如果你有一个int值的年份和月份:

 months = (year2-year1)*12 + month2 - month1; 

一年12个月。

 /** * Gets number of months between two dates. * 

Months are calculated as following:

*

After calculating number of months from years and months from two dates, * if there are still any extra days, it will be considered as one more month. * For ex, Months between 2012-01-01 and 2013-02-06 will be 14 as * Total Months = Months from year difference are 12 + Difference between months in dates is 1 * + one month since day 06 in enddate is greater than day 01 in startDate. *

* @param startDate * @param endDate * @return */ public static int getMonthsBetweenDates(Date startDate, Date endDate) { if(startDate.getTime() > endDate.getTime()) { Date temp = startDate; startDate = endDate; endDate = temp; } Calendar startCalendar = Calendar.getInstance(); startCalendar.setTime(startDate); Calendar endCalendar = Calendar.getInstance(); endCalendar.setTime(endDate); int yearDiff = endCalendar.get(Calendar.YEAR)- startCalendar.get(Calendar.YEAR); int monthsBetween = endCalendar.get(Calendar.MONTH)-startCalendar.get(Calendar.MONTH) +12*yearDiff; if(endCalendar.get(Calendar.DAY_OF_MONTH) >= startCalendar.get(Calendar.DAY_OF_MONTH)) monthsBetween = monthsBetween + 1; return monthsBetween; }

我只需要获取Calendar实例的年和月字段,将年份转换为月份并获得差异。

 private static int monthsBetween(final Date s1, final Date s2) { final Calendar d1 = Calendar.getInstance(); d1.setTime(s1); final Calendar d2 = Calendar.getInstance(); d2.setTime(s2); int diff = (d2.get(Calendar.YEAR) - d1.get(Calendar.YEAR)) * 12 + d2.get(Calendar.MONTH) - d1.get(Calendar.MONTH); return diff; } 

它也适用于闰年

 public static int getNumberOfMonths(Date fromDate, Date toDate) { int monthCount = 0; Calendar cal = Calendar.getInstance(); cal.setTime(fromDate); int c1date = cal.get(Calendar.DATE); int c1month = cal.get(Calendar.MONTH); int c1year = cal.get(Calendar.YEAR); cal.setTime(toDate); int c2date = cal.get(Calendar.DATE); int c2month = cal.get(Calendar.MONTH); int c2year = cal.get(Calendar.YEAR); System.out.println(" c1date:"+c1date+" month:"+c1month+" year:"+c1year); System.out.println(" c2date:"+c2date+" month:"+c2month+" year:"+c2year); GregorianCalendar grCal = new GregorianCalendar(); boolean isLeapYear1 = grCal.isLeapYear(c1year); boolean isLeapYear2 = grCal.isLeapYear(c2year); monthCount = ((c2year - c1year) * 12) + (c2month - c1month); if(isLeapYear2 && c2month == 1 && c2date == 29){ monthCount = monthCount+ ((c1date == 28)?0:1); }else if(isLeapYear1 && c1month == 1 && c1date == 29){ monthCount = monthCount+ ((c2date == 28)?0:1); }else{ monthCount = monthCount+ ((c2date >= c1date)?0:1); } return monthCount; } 

这会让你在几个月内获得差异

 (endCal.get(Calendar.YEAR)*12+endCal.get(Calendar.MONTH))-(startCal.get(Calendar.YEAR)*12+startCal.get(Calendar.MONTH))