根据初始时间和周期时间自动计算最近的下一周期

public Date nextCycleTime(String pczqDw, String pcZq, Date cspcDtm, Date nowTime) {
    int pczq = Integer.parseInt(pcZq);
    Date nextCycleTime = new Date();
    long dd;
    long cycleNum;
    long diff;
    if (String.valueOf(1).equals(pczqDw)) {
        dd = (nowTime.getTime() - cspcDtm.getTime()) / 3600000L;
        cycleNum = dd / (long)pczq + 1L;
        diff = cycleNum * (long)pczq;
        nextCycleTime.setTime(cspcDtm.getTime() + diff * 3600000L);
    } else if (String.valueOf(2).equals(pczqDw)) {
        dd = (nowTime.getTime() - cspcDtm.getTime()) / 86400000L;
        cycleNum = dd / (long)pczq + 1L;
        diff = cycleNum * (long)pczq;
        nextCycleTime.setTime(cspcDtm.getTime() + diff * 86400000L);
    } else {
        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();
        cal1.setTime(cspcDtm);
        cal2.setTime(nowTime);
        int cycleNum;
        int yy;
        int diff;
        if (String.valueOf(3).equals(pczqDw)) {
            yy = this.betweenMonthByTwoCalendar(cal1, cal2);
            cycleNum = yy / pczq + 1;
            diff = cycleNum * pczq;
            int diff1 = diff + cal1.get(2);
            int year = diff1 / 12;
            cal1.roll(1, year);
            int month = diff1 % 12;
            cal1.roll(2, month);
        } else if (String.valueOf(4).equals(pczqDw)) {
            yy = this.betweenYearByTwoCalendar(cal1, cal2);
            cycleNum = yy / pczq + 1;
            diff = cycleNum * pczq;
            cal1.roll(1, diff);
        }

        Date newDate = cal1.getTime();
        nextCycleTime.setTime(newDate.getTime());
    }

    return nextCycleTime;
}

private int betweenMonthByTwoCalendar(Calendar startCalendar, Calendar endCalendar) {
    if (startCalendar.after(endCalendar)) {
        Calendar Temp = startCalendar;
        startCalendar = endCalendar;
        endCalendar = Temp;
    }

    int startYear = startCalendar.get(1);
    int endYear = endCalendar.get(1);
    int startMonth = startCalendar.get(2);
    int endMonth = endCalendar.get(2);
    int flag = this.monthFlag(startCalendar, endCalendar);
    int monthNum = (endYear - startYear) * 12 + (endMonth - startMonth) - flag;
    return monthNum;
}

private int betweenYearByTwoCalendar(Calendar startCalendar, Calendar endCalendar) {
    if (startCalendar.after(endCalendar)) {
        Calendar Temp = startCalendar;
        startCalendar = endCalendar;
        endCalendar = Temp;
    }

    int startYear = startCalendar.get(1);
    int endYear = endCalendar.get(1);
    int flag = 0;
    int startMonth = startCalendar.get(2);
    int endMonth = endCalendar.get(2);
    if (endMonth == startMonth) {
        flag = this.monthFlag(startCalendar, endCalendar);
    } else if (endMonth < startMonth) {
        flag = 1;
    }

    int yearNum = endYear - startYear - flag;
    return yearNum;
}

private int monthFlag(Calendar startCalendar, Calendar endCalendar) {
    int d = endCalendar.get(5) - startCalendar.get(5);
    int h = endCalendar.get(11) - startCalendar.get(11);
    int m = endCalendar.get(12) - startCalendar.get(12);
    int s = endCalendar.get(13) - startCalendar.get(13);
    int flag = 0;
    int diffTime = d * 86400 + h * 3600 + m * 60 + s;
    if (diffTime < 0) {
        flag = 1;
    }

    return flag;
}



private int calHMS(Calendar cal) {
    return cal.get(11) * 3600 + cal.get(12) * 60 + cal.get(13);
}

public Date BCFineTuning(Date startDate, Date endDate, Date zjpcDtm) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(endDate);
    int endBC_H_M_S = this.calHMS(cal);
    cal.setTime(startDate);
    int begBC_H_M_S = this.calHMS(cal);
    cal.setTime(zjpcDtm);
    int zjpcDtm_H_M_S = this.calHMS(cal);
    if (endBC_H_M_S > begBC_H_M_S) {
        if (zjpcDtm_H_M_S < begBC_H_M_S || zjpcDtm_H_M_S >= endBC_H_M_S) {
            zjpcDtm = new Date(zjpcDtm.getTime() + (long)((begBC_H_M_S - zjpcDtm_H_M_S + 86400) * 1000));
        }
    } else if (endBC_H_M_S <= zjpcDtm_H_M_S && zjpcDtm_H_M_S < begBC_H_M_S) {
        zjpcDtm = new Date(zjpcDtm.getTime() + (long)((begBC_H_M_S - zjpcDtm_H_M_S) * 1000));
    }

    return zjpcDtm;
}

//BCFineTuning用于计算有时间范围的下一周期

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值