ChatGPT java体验

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:00
        Date 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;
    }

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值