高分求助:java统计时间间隔问题

king4693 2013-12-07 10:54:47
oracle数据库 有温度,时间2个字段
ID 温度 时间(每个时间相隔10分钟,10分钟采集一次)
1 20.4 2013-8-8 14:10:47
2 20.5 2013-8-8 14:20:47
3 20.2 2013-8-8 14:30:47
4 20.6 2013-8-8 14:40:47
5 18.4 2013-8-8 14:50:47
6 19.5 2013-8-8 15:00:47
7 17.5 2013-8-8 15:10:47
8 19.6 2013-8-8 15:20:47
9 21.6 2013-8-8 15:30:47
10 24.6 2013-8-8 15:40:47
11 25.6 2013-8-8 15:50:47
12 27.6 2013-8-8 16:00:47
13 28.6 2013-8-8 16:10:47
要求温度>20
查询后结果
1 20.4 2013-8-8 14:10:47
2 20.5 2013-8-8 14:20:47
3 20.2 2013-8-8 14:30:47
4 20.6 2013-8-8 14:40:47
9 21.6 2013-8-8 15:30:47
10 24.6 2013-8-8 15:40:47
11 25.6 2013-8-8 15:50:47
12 27.6 2013-8-8 16:00:47
13 28.6 2013-8-8 16:10:47
结果应该是
这个是一组 时间间隔是连续的
1 20.4 2013-8-8 14:10:47
2 20.5 2013-8-8 14:20:47
3 20.2 2013-8-8 14:30:47
4 20.6 2013-8-8 14:40:47
30分钟
这个是一组 时间间隔是连续的
9 21.6 2013-8-8 15:30:47
10 24.6 2013-8-8 15:40:47
11 25.6 2013-8-8 15:50:47
12 27.6 2013-8-8 16:00:47
13 28.6 2013-8-8 16:10:47
40分钟
求最大时间段
时间从2013-8-8 15:30:47-2013-8-8 16:10:47
40分钟

后台或sql函数,过程都可以
哪位大神知道!在下万分感谢!!
...全文
983 30 打赏 收藏 转发到动态 举报
写回复
用AI写文章
30 条回复
切换为时间正序
请发表友善的回复…
发表回复
king4693 2013-12-24
  • 打赏
  • 举报
回复
2013-8-8 1:10:47 2013-8-8 1:20:43 2013-8-8 1:30:11 2013-8-8 1:40:47==》连续20分钟,后面时间断掉了 2013-8-8 23:40:25 2013-8-8 23:50:12 ------------------------- 2013-8-9 0:00:47 2013-8-9 0:10:33 2013-8-9 0:20:47==》连续30分钟 最长的时间段 结果 2013-8-8(x) 23:40:25(ok)-2013-8-9(x) 0:20:47(ok) 分钟没问题 可是日期2013-8-8==2013-8-9 怎么解决呢?
king4693 2013-12-24
  • 打赏
  • 举报
回复
怎么搞嘛?你有时间帮我把代码提出来!高手!!谢谢了!!
nmyangym 2013-12-24
  • 打赏
  • 举报
回复
只要放入listDate里的时间是对的,就不会有问题。 我是以Date类型存入的。
king4693 2013-12-24
  • 打赏
  • 举报
回复
你是对的,没问题!我在测下!
nmyangym 2013-12-23
  • 打赏
  • 举报
回复
12小时制这有点问题.明天再看.
nmyangym 2013-12-23
  • 打赏
  • 举报
回复
我试一下....
king4693 2013-12-23
  • 打赏
  • 举报
回复
有个小问题? 2013-8-8 1:10:47 2013-8-8 1:20:43 2013-8-8 1:30:11 2013-8-8 1:40:47==》连续20分钟,后面时间断掉了 2013-8-8 11:40:25 2013-8-8 11:50:12 2013-8-9 0:00:47 2013-8-9 0:10:33 2013-8-9 0:20:47==》连续30分钟 最长的时间段 结果 2013-8-8(x) 11:40:25(ok)-2013-8-9(x) 0:20:47(ok) 分钟没问题 可是日期2013-8-8==2013-8-9 怎么解决呢?
king4693 2013-12-23
  • 打赏
  • 举报
回复
有个小问题? 2013-8-8 1:10:47 2013-8-8 1:20:43 2013-8-8 1:30:11 2013-8-8 1:40:47==》连续20分钟,后面时间断掉了 2013-8-8 15:30:47 2013-8-8 0:40:12 2013-8-8 0:50:47 2013-8-9 1:00:33 2013-8-9 1:10:47==》连续30分钟 最长的时间段 结果 2013-8-8(x) 15:30:47(ok)-2013-8-9(x) 1:10:47(ok) 分钟没问题 可是日期2013-8-8==2013-8-9 怎么解决呢?
king4693 2013-12-23
  • 打赏
  • 举报
回复
我试下
king4693 2013-12-22
  • 打赏
  • 举报
回复
king4693 2013-12-22
  • 打赏
  • 举报
回复
你好!nmyangym,你那个是每10分钟加一次时间,相当于每隔10分就采集一次,我要的是一组连续的时间最长的时间段 数据库数据 2013-8-8 1:10:47 2013-8-8 1:20:43 2013-8-8 1:30:11 2013-8-8 1:40:47==》连续20分钟,后面时间断掉了 2013-8-8 15:30:47 2013-8-8 15:40:12 2013-8-8 15:50:47 2013-8-8 16:00:33 2013-8-8 16:10:47==》连续30分钟 最长的时间段 结果 2013-8-8 15:30:47-2013-8-8 16:10:47 这个意思 我的方法有一些缺陷 有没有高手有好的办法解决,请指教!
nmyangym 2013-12-22
  • 打赏
  • 举报
回复

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class IntrervalTime {
	public static void main(String[] args) {
		Date date = null;
		List<Date> listDate = new ArrayList<Date>();
		Calendar cal = Calendar.getInstance();
		//模拟一些数据
		cal.set(2013, 7, 8);
		cal.set(Calendar.HOUR_OF_DAY, 1);
		cal.set(Calendar.MINUTE, 10);
		cal.set(Calendar.SECOND, 47);
		date = new Date(cal.getTimeInMillis());		
		listDate.add(date);
		//cal.set(Calendar.HOUR_OF_DAY, 1);
		cal.set(Calendar.MINUTE, 20);
		cal.set(Calendar.SECOND, 43);
		date = new Date(cal.getTimeInMillis());		
		listDate.add(date);

		//cal.set(Calendar.HOUR_OF_DAY, 1);
		cal.set(Calendar.MINUTE, 30);
		cal.set(Calendar.SECOND, 11);
		date = new Date(cal.getTimeInMillis());		
		listDate.add(date);

		cal.set(Calendar.HOUR_OF_DAY, 1);
		cal.set(Calendar.MINUTE, 40);
		cal.set(Calendar.SECOND, 47);
		date = new Date(cal.getTimeInMillis());		
		listDate.add(date);

		cal.set(Calendar.HOUR_OF_DAY, 15);
		cal.set(Calendar.MINUTE, 30);
		cal.set(Calendar.SECOND, 47);
		date = new Date(cal.getTimeInMillis());		
		listDate.add(date);
		
		cal.set(Calendar.HOUR_OF_DAY, 15);
		cal.set(Calendar.MINUTE, 40);
		cal.set(Calendar.SECOND, 12);
		date = new Date(cal.getTimeInMillis());		
		listDate.add(date);

		cal.set(Calendar.HOUR_OF_DAY, 15);
		cal.set(Calendar.MINUTE, 50);
		cal.set(Calendar.SECOND, 47);
		date = new Date(cal.getTimeInMillis());		
		listDate.add(date);

		cal.set(Calendar.HOUR_OF_DAY, 16);
		cal.set(Calendar.MINUTE, 00);
		cal.set(Calendar.SECOND, 33);
		date = new Date(cal.getTimeInMillis());		
		listDate.add(date);

		cal.set(Calendar.HOUR_OF_DAY, 16);
		cal.set(Calendar.MINUTE, 10);
		cal.set(Calendar.SECOND, 47);
		date = new Date(cal.getTimeInMillis());		
		listDate.add(date);

		
		//查看结果
		Map<Integer, Date[]> mapMaxInterval = getMapInterval(listDate);
		Set<Integer> set1 = mapMaxInterval.keySet();
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		for(Integer i: set1) {
			System.out.print("最长的时间段 结果: " + i + " minutes");
			System.out.print(" " + sdf.format(mapMaxInterval.get(i)[0]));
			System.out.println(" - " + sdf.format(mapMaxInterval.get(i)[1]));
		}
	
	}
	/**
	 * 把连续出现的时间段找出,放在一个,MapMap<Integer, Date[]> 容器,Integer 是查找到的顺序,0,1,2 。。。
	 * Date[]  是一个二维数组,第一个是起始时间, 第二个是结束时间。
	 * @param listDate
	 * @return
	 */
	public static Map<Integer, Date[]> getMapResult(List<Date> listDate) {
		//容器定义保存结果
		//
		Map<Integer, Date[]> mapResult = new HashMap<Integer, Date[]>();
		Date[] dateArr = new Date[2];
		//初始化起始参数
		//
		dateArr[0] = listDate.get(0);
		long timeMinuteBegin = dateArr[0].getTime();
		long timeMinuteEnd = 0;
		Integer index = 0;
		//连续时间的累加计数
		//
		int j = 0;
		for(int i = 1 ; i < listDate.size(); i++) {
			timeMinuteEnd = listDate.get(i).getTime();
			long interverl = timeMinuteEnd - timeMinuteBegin - j * 10 * 60 *1000;
			if(interverl > 9 * 60 *1000 && interverl < 11 * 60 *1000) {//去除时间的波动。10分钟前后1分钟,算正常。
				j++;		//如果后续时间正好连续,计数加1。
				if(i == listDate.size() -1 ) {//整个序列最后一个时间记录,放到数组里。存入容器。(结束查找了)。
					dateArr[1] = listDate.get(i);
					mapResult.put(index++ , dateArr);
				}
				continue;

			}
			else {		//出现非连续时间,上一组保存。 初始化下一组。
				//保存
				dateArr[1] = listDate.get(i - 1);
				mapResult.put(index++ , dateArr);
				dateArr = new Date[2];//一定要new 新的数组。
				//初始化
				dateArr[0] = listDate.get(i);
				timeMinuteBegin = dateArr[0].getTime();
				j = 0;
			}
		}
		return mapResult;
	}
	
	/**
	 * 找最大间隔的一组,借助于方法getMapResult(List<Date> listDate)。
	 * 扎到后,存入容器,Map<Integer, Date[]>, 其中integer是时间间隔的分钟数。
	 * Date数组是保存起始和结束时间。
	 * @param listDate
	 * @return
	 */
	public static Map<Integer, Date[]> getMapInterval(List<Date> listDate) {
		Map<Integer, Date[]> mapResult = getMapResult(listDate);
		Date[] resultDate = new Date[2];
		int intervalMax = 0;
		int interval = 0;
		Set<Integer> set = mapResult.keySet();
		for(Integer i : set) {
			interval = (int)(mapResult.get(i)[1].getTime() -  mapResult.get(i)[0].getTime())/(60 *1000);
			if(interval > intervalMax) {
				intervalMax = interval;
				resultDate = mapResult.get(i);
			}
		}
		Map<Integer, Date[]> returnMap = new HashMap<Integer, Date[]>();
		returnMap.put(intervalMax, resultDate);
		return returnMap;
	}
}

最长的时间段 结果: 40 minutes 2013-08-08 15:30:47 - 2013-08-08 16:10:47
nmyangym 2013-12-22
  • 打赏
  • 举报
回复
我的代码可以解决你的问题,时间断了,就结束了一个连续段落。 从下一条开始新的计算。
king4693 2013-12-21
  • 打赏
  • 举报
回复
各位 问题我自己已经解决了,不管怎样还是谢谢大家给我的帮助!谢谢!
nmyangym 2013-12-14
  • 打赏
  • 举报
回复
结果: max interval is : 70minutes begin time is : Sat Dec 14 15:03:31 CST 2013 end time is : Sat Dec 14 16:13:31 CST 2013
nmyangym 2013-12-14
  • 打赏
  • 举报
回复
写了一个,楼主参考:


import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class IntrervalTime {
	public static void main(String[] args) {
		Calendar cal = Calendar.getInstance();
		//模拟一些数据
		List<Date> listDate = new ArrayList<Date>();
		Date date = null;
		date = new Date(cal.getTimeInMillis());
		listDate.add(date);
		//System.out.println(listDate.get(0));
		cal.add(Calendar.MINUTE, 10);
		listDate.add(new Date(cal.getTimeInMillis()));
		//System.out.println(listDate.get(1));
		cal.add(Calendar.MINUTE, 10);
		listDate.add(new Date(cal.getTimeInMillis()));
		//System.out.println(listDate.get(2));
		cal.add(Calendar.MINUTE, 10);		
		listDate.add(new Date(cal.getTimeInMillis()));
		cal.add(Calendar.MINUTE, 10);
		listDate.add(new Date(cal.getTimeInMillis()));
		
		cal.add(Calendar.MINUTE, 20);
		listDate.add(new Date(cal.getTimeInMillis()));
		cal.add(Calendar.MINUTE, 10);
		listDate.add(new Date(cal.getTimeInMillis()));
		cal.add(Calendar.MINUTE, 10);
		listDate.add(new Date(cal.getTimeInMillis()));

		cal.add(Calendar.MINUTE, 30);
		listDate.add(new Date(cal.getTimeInMillis()));
		cal.add(Calendar.MINUTE, 10);
		listDate.add(new Date(cal.getTimeInMillis()));
		cal.add(Calendar.MINUTE, 10);
		listDate.add(new Date(cal.getTimeInMillis()));
		cal.add(Calendar.MINUTE, 10);
		listDate.add(new Date(cal.getTimeInMillis()));

		cal.add(Calendar.MINUTE, 20);
		listDate.add(new Date(cal.getTimeInMillis()));

		cal.add(Calendar.MINUTE, 10);
		listDate.add(new Date(cal.getTimeInMillis()));
		cal.add(Calendar.MINUTE, 10);
		listDate.add(new Date(cal.getTimeInMillis()));
		cal.add(Calendar.MINUTE, 10);		
		listDate.add(new Date(cal.getTimeInMillis()));
		cal.add(Calendar.MINUTE, 10);
		listDate.add(new Date(cal.getTimeInMillis()));

		cal.add(Calendar.MINUTE, 10);
		listDate.add(new Date(cal.getTimeInMillis()));
		cal.add(Calendar.MINUTE, 10);		
		listDate.add(new Date(cal.getTimeInMillis()));
		cal.add(Calendar.MINUTE, 10);
		listDate.add(new Date(cal.getTimeInMillis()));
		//查看结果
		Map<Integer, Date[]> mapMaxInterval = getMapInterval(listDate);
		Set<Integer> set1 = mapMaxInterval.keySet();
		for(Integer i: set1) {
			System.out.println("max interval is : " + i + "minutes");
			System.out.println("begin time is : " + mapMaxInterval.get(i)[0]);
			System.out.println("end time is : " + mapMaxInterval.get(i)[1]);
		}
	
	}
	/**
	 * 把连续出现的时间段找出,放在一个,MapMap<Integer, Date[]> 容器,Integer 是查找到的顺序,0,1,2 。。。
	 * Date[]  是一个二维数组,第一个是起始时间, 第二个是结束时间。
	 * @param listDate
	 * @return
	 */
	public static Map<Integer, Date[]> getMapResult(List<Date> listDate) {
		//容器定义保存结果
		//
		Map<Integer, Date[]> mapResult = new HashMap<Integer, Date[]>();
		Date[] dateArr = new Date[2];
		//初始化起始参数
		//
		dateArr[0] = listDate.get(0);
		long timeMinuteBegin = dateArr[0].getTime();
		long timeMinuteEnd = 0;
		Integer index = 0;
		//连续时间的累加计数
		//
		int j = 0;
		for(int i = 1 ; i < listDate.size(); i++) {
			timeMinuteEnd = listDate.get(i).getTime();
			long interverl = timeMinuteEnd - timeMinuteBegin - j * 10 * 60 *1000;
			if(interverl > 9 * 60 *1000 && interverl < 11 * 60 *1000) {//去除时间的波动。10分钟前后1分钟,算正常。
				j++;		//如果后续时间正好连续,计数加1。
				if(i == listDate.size() -1 ) {//整个序列最后一个时间记录,放到数组里。存入容器。(结束查找了)。
					dateArr[1] = listDate.get(i);
					mapResult.put(index++ , dateArr);
				}
				continue;

			}
			else {		//出现非连续时间,上一组保存。 初始化下一组。
				//保存
				dateArr[1] = listDate.get(i - 1);
				mapResult.put(index++ , dateArr);
				dateArr = new Date[2];//一定要new 新的数组。
				//初始化
				dateArr[0] = listDate.get(i);
				timeMinuteBegin = dateArr[0].getTime();
				j = 0;
			}
		}
		return mapResult;
	}
	
	/**
	 * 找最大间隔的一组,借助于方法getMapResult(List<Date> listDate)。
	 * 扎到后,存入容器,Map<Integer, Date[]>, 其中integer是时间间隔的分钟数。
	 * Date数组是保存起始和结束时间。
	 * @param listDate
	 * @return
	 */
	public static Map<Integer, Date[]> getMapInterval(List<Date> listDate) {
		Map<Integer, Date[]> mapResult = getMapResult(listDate);
		Date[] resultDate = new Date[2];
		int intervalMax = 0;
		int interval = 0;
		Set<Integer> set = mapResult.keySet();
		for(Integer i : set) {
			interval = (int)(mapResult.get(i)[1].getTime() -  mapResult.get(i)[0].getTime())/(60 *1000);
			if(interval > intervalMax) {
				intervalMax = interval;
				resultDate = mapResult.get(i);
			}
		}
		Map<Integer, Date[]> returnMap = new HashMap<Integer, Date[]>();
		returnMap.put(intervalMax, resultDate);
		return returnMap;
	}
}
king4693 2013-12-13
  • 打赏
  • 举报
回复
不行!!!!!
king4693 2013-12-13
  • 打赏
  • 举报
回复
时间点的List?什么意思?代码怎么搞?没懂??新手,请见谅!!!
rencht 2013-12-13
  • 打赏
  • 举报
回复
引用 12 楼 king4693 的回复:
Oracle 时间字段 varchar2(20) 2013-8-8 1:10:47 2013-8-8 1:20:47 2013-8-8 1:30:47 2013-8-8 1:40:47 2013-8-8 15:30:47 2013-8-8 15:40:47 2013-8-8 15:50:47 2013-8-8 16:00:47 2013-8-8 16:10:47 sql 查询出来结果,这样就可以了 date1 date2 2013-8-8 1:10:47 2013-8-8 1:40:47 2013-8-8 15:30:47 2013-8-8 16:10:47 就是这个意思,剩余的我可以在后台解决
我的意思是先查出来时间点的List, 然后对List做处理,处理成你要的时间对date1和date2的形式, 之后再调你自己的方法; 为什么执着于用SQL得到date1和date2这样
king4693 2013-12-13
  • 打赏
  • 举报
回复
Oracle 时间字段 varchar2(20) 2013-8-8 1:10:47 2013-8-8 1:20:47 2013-8-8 1:30:47 2013-8-8 1:40:47 2013-8-8 15:30:47 2013-8-8 15:40:47 2013-8-8 15:50:47 2013-8-8 16:00:47 2013-8-8 16:10:47 sql 查询出来结果,这样就可以了 date1 date2 2013-8-8 1:10:47 2013-8-8 1:40:47 2013-8-8 15:30:47 2013-8-8 16:10:47 就是这个意思,剩余的我可以在后台解决
加载更多回复(9)

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧