问题描述
- java计算两个日期间有多少自然周
-
给定一个开始日期和一个结束日期,开始日期如果是周六或周天,那么这就不算一周。结束日期哪怕是周一也要算是一周。最好能有代码,谢谢!
解决方案
有点小失误,修改过的
public static int get(Date dtS, Date dtE) {
Calendar calS = Calendar.getInstance();
calS.setTime(dtS);
boolean isMonday = true;
Calendar calE = Calendar.getInstance();
calE.setTime(dtE);
if(calS.get(Calendar.DAY_OF_WEEK)!=Calendar.MONDAY){//不是周一不计入
calS.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
return (int) (Math.ceil((calE.getTimeInMillis()-calS.getTimeInMillis())/(86400000.0*7))-1);
}else{
return (int) (Math.ceil((calE.getTimeInMillis()-calS.getTimeInMillis())/(86400000.0*7)));
}
}
解决方案二:
java计算两个日期间隔
java中计算日期间隔
计算两个日期间的差
解决方案三:
问题问的不太容易理解呀,你是想要只要是从周一开始就算是一周么
public static int get(Date dtS, Date dtE) {
Calendar calS = Calendar.getInstance();
calS.setTime(dtS);
if(calS.get(Calendar.DAY_OF_WEEK)!=Calendar.MONDAY){//不是周一不计入
calS.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
}
Calendar calE = Calendar.getInstance();
calE.setTime(dtE);
return (int) (Math.ceil((calE.getTimeInMillis()-calS.getTimeInMillis())/(86400000.0*7))-1);
}
时间: 2024-12-24 11:38:37