简化如下代码:int span = CURRENT_SPAN;//每次收取2天邮件 int receiveCount = 0;//获取邮件总数 Date lastExecuteTime = currentMaxTime; Date beginTime = currentMaxTime; Calendar calendar = Calendar.getInstance(); calendar.setTime(beginTime); calendar.add(Calendar.DATE, span); Date endTime = calendar.getTime(); boolean hasMore = true; while (hasMore) { if (endTime.compareTo(now) > 0) { hasMore = false; endTime = now; } List<Email> emails = null; try { emails = emailFolderTree.getEmails(beginTime, endTime); saveEmails(sysUser, emails); receiveCount += emails.size(); lastExecuteTime = endTime; calendar.setTime(endTime); calendar.add(Calendar.SECOND, 1); beginTime = calendar.getTime(); calendar.add(Calendar.DATE, span); endTime = calendar.getTime(); } catch (Exception e) { logger.error(e.getMessage()); // 增加对异常信息的处理 if(e.getMessage()==null){ personalEmailTaskTime.setRemark(""); } if(e.getMessage()!=null){ if(e.getMessage().length()<100){ personalEmailTaskTime.setRemark(e.getMessage()); } if(e.getMessage().length()>100){ personalEmailTaskTime.setRemark(e.getMessage().substring(0,100)); } } break; } } personalEmailTaskTime.setLastExecuteTime(lastExecuteTime);
时间: 2023-06-05 09:05:28 浏览: 153
可以简化的代码如下:
int span = CURRENT_SPAN;
int receiveCount = 0;
Date lastExecuteTime = currentMaxTime;
Date beginTime = currentMaxTime;
while (beginTime.compareTo(now) < 0) {
Date endTime = new Date(beginTime.getTime() + span * 86400000L);
if (endTime.compareTo(now) > 0) {
endTime = now;
}
List<Email> emails = null;
try {
emails = emailFolderTree.getEmails(beginTime, endTime);
saveEmails(sysUser, emails);
receiveCount += emails.size();
lastExecuteTime = endTime;
} catch (Exception e) {
logger.error(e.getMessage());
if(e.getMessage()==null){
personalEmailTaskTime.setRemark("");
}
if(e.getMessage()!=null){
if(e.getMessage().length()<100){
personalEmailTaskTime.setRemark(e.getMessage());
}
if(e.getMessage().length()>100){
personalEmailTaskTime.setRemark(e.getMessage().substring(0,100));
}
}
break;
}
beginTime = new Date(endTime.getTime() + 1000);
}
personalEmailTaskTime.setLastExecuteTime(lastExecuteTime);
这个代码简化了原来的代码逻辑,去除了无用的变量和重复的计算,同时更加精简和易读。
阅读全文