当前位置:编程学习 > JAVA >>

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

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函数,过程都可以
哪位大神知道!在下万分感谢!!  --------------------编程问答-------------------- 你是想时间以连续不连续进行分组查询? --------------------编程问答-------------------- 是,请问怎么写?  --------------------编程问答-------------------- select max(id),温度  ,时间 from 表 group by  温度  ,时间 --------------------编程问答--------------------
引用 3 楼 u012463264 的回复:
select max(id),温度  ,时间 from 表 group by  温度  ,时间

时间要排序吧 --------------------编程问答-------------------- ??不是排序问题,要考虑时间间隔是连续性 --------------------编程问答-------------------- 嘛意思 --------------------编程问答-------------------- 除 --------------------编程问答-------------------- oracle数据库 时间字段 
查询后结果
2013-8-8 14:10:47
2013-8-8 14:20:47
2013-8-8 14:30:47
2013-8-8 14: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

这个是一组 时间间隔是连续的
2013-8-8 14:10:47
2013-8-8 14:20:47
2013-8-8 14:30:47
2013-8-8 14: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 14:10:47                2013-8-8 14:40:47
2013-8-8 15:30:47                2013-8-8 16:10:47
把时间连续的分成一组,是Oracle数据库 应该要用row_number() 函数

剩下的我会在后台解决!!
谢谢! --------------------编程问答-------------------- select a.时间 as date1 from 表 as a, 表 as b
where a.温度>=20 and b.温度<20 and b.时间=a.时间-(10分钟)

select a.时间 as date1 from 表 as a, 表 as b
where a.温度>=20 and b.温度<20 and b.时间=a.时间+(10分钟)

放一个时间为"最小时间-10分钟"的记录,让第一组可以查到
不知道以后的温度是否会超过20,最后一组不查
时间间隔不稳定的话,时间条件改为范围,实在不行就用id... --------------------编程问答-------------------- 不行!!!!! --------------------编程问答-------------------- 一定要用SQL直接把结果查出来么= =...

你自己也分了步骤了,
先把温度>20的时间都查出来;
然后对时间分组,时间连续的分在一组里;
之后再...
像这样不行? --------------------编程问答-------------------- 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
就是这个意思,剩余的我可以在后台解决 --------------------编程问答--------------------
引用 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这样 --------------------编程问答-------------------- 时间点的List?什么意思?代码怎么搞?没懂??新手,请见谅!!! --------------------编程问答-------------------- 写了一个,楼主参考:


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;
}
}
--------------------编程问答-------------------- 结果:
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,你那个是每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
这个意思 
我的方法有一些缺陷
有没有高手有好的办法解决,请指教!


--------------------编程问答--------------------
--------------------编程问答-------------------- 我的代码可以解决你的问题,时间断了,就结束了一个连续段落。
从下一条开始新的计算。 --------------------编程问答--------------------

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

--------------------编程问答-------------------- 我试下 --------------------编程问答-------------------- 有个小问题?
 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 怎么解决呢?
--------------------编程问答-------------------- 有个小问题?
   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 怎么解决呢? --------------------编程问答-------------------- 我试一下.... --------------------编程问答-------------------- 12小时制这有点问题.明天再看. --------------------编程问答-------------------- 只要放入listDate里的时间是对的,就不会有问题。
我是以Date类型存入的。 --------------------编程问答-------------------- 怎么搞嘛?你有时间帮我把代码提出来!高手!!谢谢了!! --------------------编程问答--------------------    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 怎么解决呢?  --------------------编程问答-------------------- 你是对的,没问题!我在测下!

补充:Java ,  Web 开发
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,