本帖最后由 doyxy 于 2014-3-28 23:07 编辑
想做一个程序获取两个日期之间的工作日和休息日天数,当天数相差2天以内而且前面的日期大于后面的日期时,返回值错误,
比如 2014.03.26 - 2014.03.25 时,输出4,-4,
如果正序则正常,
发觉同一个星期时判断有问题,但是想不出怎么处理,求教应该怎么处理,谢谢!
- import java.util.Calendar;
- import java.util.Date;
- public class Improve {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- // 练习4,获取两个日期间的工作天数和休息天数,包含头不包含尾.
- // System.out.println("workdays:" + getWorkdays(2014, 2, 1, 2014, 1,
- // 31));
- Calendar c1 = Calendar.getInstance();
- Calendar c2 = Calendar.getInstance();
- c1.set(2014, 2, 26);
- c2.set(2014, 2, 25);
- int workdays = getWorkdays(c1.getTime(), c2.getTime());
- int restdays = getrestdays(c1.getTime(), c2.getTime());
- System.out.println(c1.getTime());
- System.out.println(c2.getTime());
- System.out.println("workdays:" + workdays);
- System.out.println("restdays:" + restdays);
- }
- private static int getrestdays(Date d1, Date d2) {
- // TODO Auto-generated method stub
- return (getTotaleDays(d1, d2)-getWorkdays(d1, d2));
- }
- private static int getWorkdays(Date d1, Date d2) {
- // TODO Auto-generated method stub
- // 获取两个日期间的总天数.
- // 判断第一周和最后一周包含多少工作日和休息日 ow1,or1,ow2,or2.
- // 总天数减去头尾两周的天数(ow1+or1+ow2+ow2)就是包含完整星期的天数,
- // 除以7即得星期数,*5为工作日,*2为休息日,加上上面的工作日和休息日即得总的天数.
- int workDays;
- Calendar c1 = Calendar.getInstance();
- Calendar c2 = Calendar.getInstance();
-
- c1.setTime(d1);
- c2.setTime(d2);
- int days = getTotaleDays(d1, d2);//获取总天数
- int ow1, or1, ow2, or2;
- // 当两个时间在同一星期时出错
- // 获取开始日期是星期几
- int weekC1 = c1.get(Calendar.DAY_OF_WEEK);// 1-7代表星期日-星期六,
- int weekC2 = c2.get(Calendar.DAY_OF_WEEK);
- /*
- * 第一周
- * 1-SUNDAY ow+5 or+2
- * 2-MONDAY ow+5 or+1
- * 3-TUESDAY ow+4 or+1
- * 4-WEDNESDAY ow+3 or+1
- * 5-THURSDAY ow+2 or+1
- * 6-FRIDAY ow+1 or+1
- * 7-SATURDAY ow+0 or+1
- * 除了SUNDAY ow = 7-weekC1,or=1;
- */
- // 获取第一周的工作日ow1和休息日or1天数.
- if (weekC1 == 1) {
- ow1 = 5;
- or1 = 2;
- } else {
- ow1 = 7 - weekC1;
- or1 = 1;
- }
- /*
- * 最后一周,因为不包含最后一天
- * 1-SUNDAY ow+0 or+0
- * 2-MONDAY ow+0 or+1
- * 3-TUESDAY ow+1 or+1
- * 4-WEDNESDAY ow+2 or+1
- * 5-THURSDAY ow+3 or+1
- * 6-FRIDAY ow+4 or+1
- * 7-SATURDAY ow+5 or+1
- * 除了SUNDAY ow=weekC2-2, or=1;
- */
- // 获取最后一周的工作日ow2和休息日or2天数.
- if (weekC2 == 1) {
- ow2 = 0;
- or2 = 0;
- } else {
- ow2 = weekC2 - 2;
- or2 = 1;
- }
- int fullWeeks;
- // 完整星期的天数
- fullWeeks = (days - ow1 - or1 - ow2 - or2) / 7;
- // System.out.println("完整星期数:"+fullWeeks);
- // System.out.println("ow1="+ow1+",ow2="+ow2);
- workDays = fullWeeks * 5 + ow1 + ow2;
-
- return workDays;
- }
- private static int getTotaleDays(Date d1, Date d2) {
- Calendar c1 = Calendar.getInstance();
- Calendar c2 = Calendar.getInstance();
- c1.setTime(d1);
- c2.setTime(d2);
- // 判断c1和c2的大小,保证c1在前,c2在后.
- if (c1.compareTo(c2) > 0) {
- c1.setTime(d2);
- c2.setTime(d1);
- System.out.println("c1>c2");
- }
- // 获取c1和c2间的天数
- int days = (int) ((c2.getTimeInMillis() - c1.getTimeInMillis()) / (1000 * 3600 * 24));
- // System.out.println(days);
- return days;
- }
-
- }
复制代码
|