java 如何使用日历api在java中查找两个日期范围之间的月数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27181307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find number of months between two date range in java using calender api
提问by John
I have two dates (01/01/2012, 31/07/2014).
我有两个日期 (01/01/2012, 31/07/2014)。
Would you please help me to calculate month difference between this two dates. If difference is 8 months 1 days i need result 9 months.
请你帮我计算这两个日期之间的月差。如果差异是 8 个月 1 天,我需要结果 9 个月。
回答by Md. Kamruzzaman
Working Code:
public int monthsBetweenDates(Date startDate, Date endDate){
Calendar start = Calendar.getInstance();
start.setTime(startDate);
Calendar end = Calendar.getInstance();
end.setTime(endDate);
int monthsBetween = 0;
int dateDiff = end.get(Calendar.DAY_OF_MONTH)-start.get(Calendar.DAY_OF_MONTH);
if(dateDiff<0) {
int borrrow = end.getActualMaximum(Calendar.DAY_OF_MONTH);
dateDiff = (end.get(Calendar.DAY_OF_MONTH)+borrrow)-start.get(Calendar.DAY_OF_MONTH);
monthsBetween--;
if(dateDiff>0) {
monthsBetween++;
}
}
else {
monthsBetween++;
}
monthsBetween += end.get(Calendar.MONTH)-start.get(Calendar.MONTH);
monthsBetween += (end.get(Calendar.YEAR)-start.get(Calendar.YEAR))*12;
return monthsBetween;
}
回答by Elliott Frisch
You might use a Calendar
like,
你可能会使用一个Calendar
赞,
static int monthsBetween(Date a, Date b) {
Calendar cal = Calendar.getInstance();
if (a.before(b)) {
cal.setTime(a);
} else {
cal.setTime(b);
b = a;
}
int c = 0;
while (cal.getTime().before(b)) {
cal.add(Calendar.MONTH, 1);
c++;
}
return c - 1;
}
then you could call it like
那么你可以这样称呼它
public static void main(String[] args) {
String start = "01/01/2012";
String end = "31/07/2014";
DateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
try {
System.out.println(monthsBetween(sdf.parse(start), sdf.parse(end)));
} catch (ParseException e) {
e.printStackTrace();
}
}
Output is
输出是
30
Or, using Joda-Time
或者,使用Joda-Time
String start = "01/01/2012";
String end = "31/07/2014";
DateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
try {
LocalDate a = LocalDate.fromDateFields(sdf.parse(start));
LocalDate b = LocalDate.fromDateFields(sdf.parse(end));
Period p = new Period(a, b);
System.out.println((p.getYears() * 12) + p.getMonths());
} catch (ParseException e) {
e.printStackTrace();
}
Output is also
输出也是
30
Edit
编辑
Finally (as suggested in the comments), if you're using Java 8 you might use the new java.time
classes like
最后(如评论中所建议),如果您使用的是 Java 8,您可能会使用新java.time
类,例如
String start = "01/01/2012";
String end = "31/07/2014";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDate from = LocalDate.parse(start, formatter);
LocalDate to = LocalDate.parse(end, formatter);
System.out.println(from.until(to, ChronoUnit.MONTHS));
Output is (still)
输出是(仍然)
30
回答by Lin
My code:
我的代码:
int getMonthCountBetween(Calendar cal1, Calendar cal2) {
if(cal1.compareTo(cal2)>0) {
Calendar cal=cal1;
cal1=cal2;
cal2=cal;
}
int months=0;
int y1 = cal1.get(Calendar.YEAR);
int y2 = cal2.get(Calendar.YEAR);
int m1 = cal1.get(Calendar.MONTH);
int m2 = cal2.get(Calendar.MONTH);
if (y2 - y1 >= 0) {
months = m2 - m1 + 1;
}
if (y2 - y1 >= 1) {
months += 12;
}
if (y2 - y1 >= 2) {
months += 12 * (y2 - y1 - 1);
}
return months;
}
回答by Upender Reddy
int months = 12*(toYear-fromYear)+(toMonth-fromMonth)+(toDay>=fromDay?1:0)
Where to is the later date and from is prior date
哪里是较晚的日期,from 是较早的日期