java for循环中两种判断方式的比较记录

记录下在开发月历显示时,客户要求需要把取消的例会在月历上排除掉,下面张贴下两种排除方式的比较,第二种逻辑上是错误的,会出现重复。其实原因还是自己的逻辑性思维不够好。

第一种,正确:

//排除被取消的例会
if (!CollectionUtils.isEmpty(zhbg_hysgl_xxgl_qxydList)) {//如果存在被取消的例会则进入处理,排除
    boolean flag = false;
    for (int j = 0; j < zhbg_hysgl_xxgl_qxydList.size(); j++) {
        ZHBG_HYSGL_XXGL_QXYD qxyd = zhbg_hysgl_xxgl_qxydList.get(j);
        String cancelDate = new java.sql.Date(c_begin.getTime().getTime()).toString();
        if (cancelDate.equalsIgnoreCase(qxyd.getQXRQ())) {//判断例会被取消的日期和日历的日期相不相同,不相同才添加例会
            flag = true;
            break;
        }
    }
    if (!flag) {
        //这里要循环处理实体的属性
        json = new JSONObject();
        json.put("id", bean.getHYSXXGLID());
        strkssj = bean.getLHKSSJ();
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        Date sdate = sdf.parse(strkssj);
        strjssj = bean.getLHJSSJ();
        Date edate = sdf.parse(strjssj);
        bsw = sdate.getHours() <= 12;
        bxw = edate.getHours() <= 12;
        if (bsw && bxw) {
            strsw = "上午";
        } else if (sdate.getHours() > 12 && edate.getHours() > 12) {
            strsw = "下午";
        } else {
            strsw = "全天";
        }
        json.put("start", new java.sql.Date(c_begin.getTime().getTime()) + " " + bean.getLHKSSJ());
        json.put("end", new java.sql.Date(c_begin.getTime().getTime()) + " " + bean.getLHJSSJ());
        json.put("description", bean.getLHMC());
        json.put("hys", strsw + "  " + entxxgl.getHYSMC());
        json.put("sqbm", entxxgl.getSSDW_TNAME());
        json.put("chrs", entxxgl.getRNCHRS());
        json.put("sfapcs", "");
        json.put("hybztsyq", "");
        jsona.add(json);
    }
}
第二种,错误:

//排除被取消的例会
if (!CollectionUtils.isEmpty(zhbg_hysgl_xxgl_qxydList)) {//如果存在被取消的例会则进入处理,排除
    for (int j = 0; j < zhbg_hysgl_xxgl_qxydList.size(); j++) {
        ZHBG_HYSGL_XXGL_QXYD qxyd = zhbg_hysgl_xxgl_qxydList.get(j);
        String cancelDate = new java.sql.Date(c_begin.getTime().getTime()).toString();
        if (!cancelDate.equalsIgnoreCase(qxyd.getQXRQ())) {//判断例会被取消的日期和日历的日期相不相同,不相同才添加例会
            //这里要循环处理实体的属性
            json = new JSONObject();
            json.put("id", bean.getHYSXXGLID());
            strkssj = bean.getLHKSSJ();
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
            Date sdate = sdf.parse(strkssj);
            strjssj = bean.getLHJSSJ();
            Date edate = sdf.parse(strjssj);
            bsw = sdate.getHours() <= 12;
            bxw = edate.getHours() <= 12;
            if (bsw && bxw) {
                strsw = "上午";
            } else if (sdate.getHours() > 12 && edate.getHours() > 12) {
                strsw = "下午";
            } else {
                strsw = "全天";
            }
            json.put("start", new java.sql.Date(c_begin.getTime().getTime()) + " " + bean.getLHKSSJ());
            json.put("end", new java.sql.Date(c_begin.getTime().getTime()) + " " + bean.getLHJSSJ());
            json.put("description", bean.getLHMC());
            json.put("hys", strsw + "  " + entxxgl.getHYSMC());
            json.put("sqbm", entxxgl.getSSDW_TNAME());
            json.put("chrs", entxxgl.getRNCHRS());
            json.put("sfapcs", "");
            json.put("hybztsyq", "");
            jsona.add(json);
        }
    }
}

你可能感兴趣的:(JavaEE-综合,Java-综合)