1.可以帮你实现设计模式模型
2.可以帮你优化代码,比如下面这段代码获取时间集合的方法
public List<Date> getDealTimeList(Integer showType){List<Date> timeList = new ArrayList<>();Calendar cal = new GregorianCalendar();cal.set(Calendar.HOUR_OF_DAY, 0);cal.set(Calendar.MINUTE, 0);cal.set(Calendar.SECOND, 0);cal.set(Calendar.MILLISECOND, 0);//取当天 00:00:00Date date = cal.getTime();int count = 0;int minute = 0;if (showType == RateShowTimeTypeEnum.MINUTE.getType()){//0点开始15分钟一个点minute = 15;//循环取前1天时间 24小时乘60分除15等于循环次数count = 24 * 60 / 15;}else if (showType == RateShowTimeTypeEnum.HOURS.getType()){//1天 1小时一个点minute= 60;//1天24小时循环24次count = 24;}for (int i = 0; i< count; i++){Calendar calendar = Calendar.getInstance();calendar.setTime(date);calendar.add(Calendar.MINUTE, + minute);Date beforeDate = calendar.getTime();//大于当前时间则退出if (beforeDate.compareTo(new Date()) > 0){break;}timeList.add(beforeDate);}return timeList;}
好家伙,不单是给分析优化方向,连优化后的代码都给写出来了(注释都给加好了)这不CV直接用了?
public List<Date> getDealTimeListTwo(Integer showType){List<Date> timeList = new ArrayList<>();ZonedDateTime now = ZonedDateTime.now();ZonedDateTime start = now.with(ChronoField.MINUTE_OF_DAY, 0).with(ChronoField.SECOND_OF_MINUTE, 0).with(ChronoField.MILLI_OF_SECOND, 0);Duration duration;if (showType == RateShowTimeTypeEnum.MINUTE.getType()) {duration = Duration.ofMinutes(15);} else if (showType == RateShowTimeTypeEnum.HOURS.getType()) {duration = Duration.ofHours(1);} else {// 如果不满足上述条件,则返回空列表。return timeList;}// 添加一天内所有的时间点到列表中。for (ZonedDateTime time = start; time.isBefore(now); time = time.plus(duration)) {Date date = Date.from(time.toInstant());timeList.add(date);}return timeList;}