时间遍历(按照小时)

时间遍历(按照小时),第1张

时间遍历(按照小时)
    public static void main(String[] args)  {
        // 日期格式化
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            // 起始日期
            Date begin = sdf.parse("2021-09-07 01:00:00");
            // 结束日期
            Date end = sdf.parse("2021-09-08 01:00:00");

            Calendar s1 = Calendar.getInstance();
            s1.setTime(begin);
            Calendar s2 = Calendar.getInstance();
            s2.setTime(begin);
            s2.add(Calendar.HOUR_OF_DAY, 1);
            Integer num=0;
            // 打印
            while (begin.getTime() < end.getTime()) {
                num++;
                begin= s1.getTime();
                System.out.println(sdf.format(s1.getTime())+"|| "+sdf.format(s2.getTime()));
                System.out.println(num);
                // 加上1
                s1.add(Calendar.HOUR_OF_DAY, 1);
                s2.add(Calendar.HOUR_OF_DAY, 1);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }

将所有查到的数据放入allList中,统一插入数据库

	@RequestMapping("getTruckNum")
	public ResultObj getTruckNum(@RequestParam(name="begin", required=true) String beginTime, 
			@RequestParam(name="end", required=true) String endTime){
		
		List allList=new ArrayList();
        try {
        	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            // 起始日期
            Date begin = sdf.parse(beginTime);
            // 结束日期
            Date end = sdf.parse(endTime);

            Calendar s1 = Calendar.getInstance();
            s1.setTime(begin);
            Calendar s2 = Calendar.getInstance();
            s2.setTime(begin);
            s2.add(Calendar.HOUR_OF_DAY, 1);
            
            // 打印
            while (begin.getTime() < end.getTime()) {
                begin= s1.getTime();
                          
                String t1=sdf.format(s1.getTime());
                String t2=sdf.format(s2.getTime());
                List list = invokeService.getTruckNum(t1, t2);
                for(NpicTruckActive npicTruckActive:list )
                {  
                	allList.add(npicTruckActive);
                }
              
                // 加上1
                s1.add(Calendar.HOUR_OF_DAY, 1);
                s2.add(Calendar.HOUR_OF_DAY, 1);
            }
            
            npicTruckActiveWriteMapper.insertBatch(allList);
        } catch (ParseException e) {
            e.printStackTrace();
        }
		
		
		//List list = invokeService.getTruckNum(begin, end);
		JSONObject obj = new JSONObject();
		obj.put("data", allList);
		
		return getSuccessResult(obj);
	}

发现这样allList太大了,还是查一个list,插一个list

@RequestMapping("getTruckNum")
	public ResultObj getTruckNum(@RequestParam(name="begin", required=true) String beginTime, 
			@RequestParam(name="end", required=true) String endTime){
		
        try {
        	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            // 起始日期
            Date begin = sdf.parse(beginTime);
            // 结束日期
            Date end = sdf.parse(endTime);

            Calendar s1 = Calendar.getInstance();
            s1.setTime(begin);
            Calendar s2 = Calendar.getInstance();
            s2.setTime(begin);
            s2.add(Calendar.HOUR_OF_DAY, 1);
            
            // 打印
            while (begin.getTime() < end.getTime()) {
                begin= s1.getTime();
                          
                String t1=sdf.format(s1.getTime());
                String t2=sdf.format(s2.getTime());
                List list = invokeService.getTruckNum(t1, t2);
                npicTruckActiveWriteMapper.insertBatch(list);
              
                // 加上1
                s1.add(Calendar.HOUR_OF_DAY, 1);
                s2.add(Calendar.HOUR_OF_DAY, 1);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5686917.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存