码迷,mamicode.com
首页 > 其他好文 > 详细

往前往后推时间(排除工作日和节假日)

时间:2015-11-06 14:36:21      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:

1.往后推时间

public static Date getNextWorkingDay(int period) throws DBException {
        CodeMgr codeMgr = (CodeMgr) BizMgrRegistry.getInstance().getBizMgr(
                "com.leapii.code.bm.CodeMgr");
        Collection publicHolidayCollection = codeMgr
                .getCodeByType(DORMConstants.PUBLIC_HOLIDAY);

        Calendar currentDay = Calendar.getInstance(TimeZone
                .getTimeZone("GMT+08"));
        currentDay.setTime(DateUtil.getDate());

        for (int i = 0; i < period; i++) {
            boolean isPublicHoliday = false;

            currentDay.set(Calendar.DATE, currentDay.get(Calendar.DATE) + 1);

            String currenDayStr = DateUtil.format(currentDay.getTime(),
                    "yyyyMMdd");

            System.out.println(currenDayStr);

            Iterator itr = publicHolidayCollection.iterator();
            while (itr.hasNext()) {
                Code code = (Code) itr.next();
                if (code.getValue().equals(currenDayStr)) {
                    isPublicHoliday = true;
                    break;
                }
            }

            int dayOfWeek = currentDay.get(Calendar.DAY_OF_WEEK);
            if (Calendar.SATURDAY == dayOfWeek || Calendar.SUNDAY == dayOfWeek
                    || isPublicHoliday) {
                System.out.println("Weekend Or Public Holiday");
                period = period + 1;
            }
        }

        return currentDay.getTime();
    }

 

2.往前推时间

public static Date getLastWorkingDay(int period) throws DBException {
        CodeMgr codeMgr = (CodeMgr) BizMgrRegistry.getInstance().getBizMgr(
                "com.leapii.code.bm.CodeMgr");
        Collection publicHolidayCollection = codeMgr
                .getCodeByType(DORMConstants.PUBLIC_HOLIDAY);

        Calendar currentDay = Calendar.getInstance(TimeZone
                .getTimeZone("GMT+08"));
        currentDay.setTime(DateUtil.getDate());

        for (int i = 0; i < period; i++) {
            boolean isPublicHoliday = false;

            currentDay.set(Calendar.DATE, currentDay.get(Calendar.DATE) - 1);

            String currenDayStr = DateUtil.format(currentDay.getTime(),
                    "yyyyMMdd");

            System.out.println(currenDayStr);

            Iterator itr = publicHolidayCollection.iterator();
            while (itr.hasNext()) {
                Code code = (Code) itr.next();
                if (code.getValue().equals(currenDayStr)) {
                    isPublicHoliday = true;
                    break;
                }
            }

            int dayOfWeek = currentDay.get(Calendar.DAY_OF_WEEK);
            if (Calendar.SATURDAY == dayOfWeek || Calendar.SUNDAY == dayOfWeek
                    || isPublicHoliday) {
                System.out.println("Weekend Or Public Holiday");
                period = period + 1;
            }
        }

        return currentDay.getTime();
    }

 

往前往后推时间(排除工作日和节假日)

标签:

原文地址:http://www.cnblogs.com/jianlun/p/4942283.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!