oracle在groupby时间时,时间间断问题处理
来源:互联网 发布:密立根油滴学生数据 编辑:程序博客网 时间:2024/05/17 06:35
直接贴代码:
package com.groupby;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;/** * 主要是在oracle中groupby time时,时间间断,也需要统计出来时.需要在代码中处理 * * 实际统计的结果 预期结果 * count groupByTime count groupByTime * 2 2014-12-12 2 2014-12-12 * 5 2014-12-11 5 2014-12-11 * 9 2014-12-10 9 2014-12-10 * 3 2014-12-07 0 2014-12-09 * 0 2014-12-08 * 3 2014-12-07 * * @author WeiJun Hu * */public class GroupByUtil {public static void main(String[] args) {try {String format = "yyyy-MM-dd";List<User> list = new ArrayList<User>();SimpleDateFormat sdf = new SimpleDateFormat(format);User user = null;user = new User();user.setCount(2);user.setCreateTime(sdf.parse("2014-12-12"));list.add(user);user = new User();user.setCount(5);user.setCreateTime(sdf.parse("2014-12-11"));list.add(user);user = new User();user.setCount(9);user.setCreateTime(sdf.parse("2014-12-10"));list.add(user);user = new User();user.setCount(3);user.setCreateTime(sdf.parse("2014-12-07"));list.add(user);GroupByUtil util = new GroupByUtil();list = util.getList(util.listToMap(list, format), 6, sdf.parse("2014-12-07"), format);for (User user2 : list) {System.out.println("时间:"+sdf.format(user2.getCreateTime())+" 数量:"+user2.getCount());}/**运行结果如下: * 时间:2014-12-07 数量:3时间:2014-12-08 数量:0时间:2014-12-09 数量:0时间:2014-12-10 数量:9时间:2014-12-11 数量:5时间:2014-12-12 数量:2 */} catch (Exception e) {e.printStackTrace();}}/** * 集合处理方法 * @param map 数据Map对象 * @param maxResult 集合大小 * @param startDate 开始时间 * @param format 时间格式化字符串 * @return */private List<User> getList(Map<String, Integer> map,Integer maxResult,Date startDate,String format){List<User> list = new ArrayList<User>();Calendar calendar = Calendar.getInstance();calendar.setTime(startDate);SimpleDateFormat sdf = new SimpleDateFormat(format);for(Integer i=0;i<maxResult;i++){String dataStr = sdf.format(calendar.getTime());User user = new User();Integer countNum = map.get(dataStr);if(countNum==null){user.setCount(0);}else{user.setCount(countNum);}user.setCreateTime(calendar.getTime());list.add(user);if(format.equals("yyyy-MM-dd")){calendar.add(Calendar.DAY_OF_MONTH,1);}else if(format.equals("yyyy-MM")){calendar.add(Calendar.MONTH, 1);}}return list;}/** * List集合存储至以时间字符为key值的Map对象 * @param list 集合 * @param format 时间格式化字符串 * @return 以时间字符为key值的Map对象 */private Map<String, Integer> listToMap(List<User> list,String format){Map<String, Integer> map = new HashMap<String, Integer>();SimpleDateFormat sdf = new SimpleDateFormat(format);for (User user : list) {map.put(sdf.format(user.getCreateTime()),user.getCount());}return map;}}
User类:
package com.groupby;import java.util.Date;public class User {private Integer id;private String name;private Integer count;private Date createTime;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Date getCreateTime() {return createTime;}public void setCreateTime(Date createTime) {this.createTime = createTime;}public Integer getCount() {return count;}public void setCount(Integer count) {this.count = count;}}
0 0
- oracle在groupby时间时,时间间断问题处理
- 关于oracle数据库时间处理问题
- ORACLE时间处理大全
- ORACLE时间处理大全
- oracle 时间 处理 大全
- oracle时间处理函数
- Oracle 时间处理
- ORACLE时间处理大全
- ORACLE时间处理大全
- oracle的时间处理
- ORACLE日期时间处理
- Oracle处理日期时间
- oracle时间处理
- oracle 的时间处理
- oracle 时间处理
- oracle时间处理汇总
- oracle 时间类型处理
- oracle 时间处理
- UVa 544 - Heavy Cargo
- 创业者的2014:最痛苦的是人和方向
- 国内眼动仪选购调研
- opencv7-ml之svm(3)应用
- init.d脚本和openwrt程序自启动
- oracle在groupby时间时,时间间断问题处理
- Java多线程的synchronized和Lock
- 矩阵相乘!
- 比较java新旧I/O的性能——以复制大文件为例
- asp.net 字符串前自动补0,字符串格式化
- C结构体
- ImageMagick +Jmagick安装
- acm课程
- html 框架中子窗体与父窗体之间的调用