用JAVA怎么判断两个日期之间是否为一整年,要考虑到闰年问题。有点复杂啊
3个回答
展开全部
public static boolean isOneYear(Date start, Date end) {
Calendar startday = Calendar.getInstance();
Calendar endday = Calendar.getInstance();
startday.setTime(start);
endday.setTime(end);
if (startday.after(endday)) {
return false;
}
long sl = startday.getTimeInMillis();
long el = endday.getTimeInMillis();
long days = ((el - sl) / (1000 * 60 * 60 * 24));
if (days == 365 || days == 366) {
if (startday.get(Calendar.MONTH) <= 1) {
startday.set(Calendar.MONTH, 1);
int lastDay = startday.getActualMaximum(Calendar.DAY_OF_MONTH);
return (lastDay == 28 && days == 365) || (lastDay == 29 && days == 366);
} else {
endday.set(Calendar.MONTH, 1);
int lastDay = endday.getActualMaximum(Calendar.DAY_OF_MONTH);
return (lastDay == 28 && days == 365) || (lastDay == 29 && days == 366);
}
} else {
return false;
}
}
Calendar startday = Calendar.getInstance();
Calendar endday = Calendar.getInstance();
startday.setTime(start);
endday.setTime(end);
if (startday.after(endday)) {
return false;
}
long sl = startday.getTimeInMillis();
long el = endday.getTimeInMillis();
long days = ((el - sl) / (1000 * 60 * 60 * 24));
if (days == 365 || days == 366) {
if (startday.get(Calendar.MONTH) <= 1) {
startday.set(Calendar.MONTH, 1);
int lastDay = startday.getActualMaximum(Calendar.DAY_OF_MONTH);
return (lastDay == 28 && days == 365) || (lastDay == 29 && days == 366);
} else {
endday.set(Calendar.MONTH, 1);
int lastDay = endday.getActualMaximum(Calendar.DAY_OF_MONTH);
return (lastDay == 28 && days == 365) || (lastDay == 29 && days == 366);
}
} else {
return false;
}
}
展开全部
先判断这两个日期之间是不是闰年,如果是就计算这两个之间是不是366天,如果不是就是365天。
追问
哥哥,这你都知道?我也知道啊,我要的是具体实现方法呀,而且不是你想的那么简单,如果前一个日期是闰年而过了2月份,那就不用考虑闰年了,但按照你说的还是要考虑闰年的。。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你判断一下是否大于等于365,不是的话不是一整年。是的话,再做一个判断是否大于等于366,是的话是平年,不是的话是闰年。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询