遗留代码重构疑问:month>1且周为01的条件是否可成立?能否删除?
关于遗留Java日期处理代码的疑问
我最近在重构一段遗留Java代码,提前写了五个测试用例,目前全部通过。这段待重构的方法代码如下:
public static synchronized String getWeekFromDate(Date date) { String strYyear = new SimpleDateFormat("yyyy").format(date); String strMonth = new SimpleDateFormat("MM").format(date); String strWeek = new SimpleDateFormat("ww").format(date); int month = Integer.parseInt(strMonth); if (month > 1 && "01".equals(strWeek)) { int year = Integer.parseInt(strYyear); return (year + 1) + "01"; } return new SimpleDateFormat("yyyyww").format(date); }
我对其中的条件判断if (month > 1 && "01".equals(strWeek))存在疑问,我的理解是:当日期的月份大于1月(即非1月)且属于当年第01周时,该条件成立。想请教几个问题:
- 我的这个理解是否正确?
- 这个条件是否有实际意义?
- 我是否可以安全删除这段代码而不产生影响?
目前已经通过的测试用例包括:
- 输入
2018-01-03,应返回"201801" - 输入
2018-02-01,应返回"201805" - 输入
2018-08-15,应返回"201833"
内容的提问来源于stack exchange,提问作者Héctor Valls
相关产品推荐
相关产品推荐

