directly paste code:
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; /** * is mainly in Oracle GroupBy time, the gap, also need to be counted. The results expected in the code to be processed * The actual statistics * Count GroupBy Time 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) {TR
y {String format = "YYYY-MM-DD";
list<user> list = new arraylist<user> ();
SimpleDateFormat SDF = new SimpleDateFormat (format);
User user = null; user = newUser ();
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 ("Time:" +sdf.format (User2.getcreatetime ()) + "Quantity:" +user2.getcount ());
The results of/** operation are as follows: * Time: 2014-12-07 Quantity: 3 time: 2014-12-08 Quantity: 0
Time: 2014-12-09 Qty: 0 Time: 2014-12-10 Quantity: 9 Time: 2014-12-11 Quantity: 5 time: 2014-12-12 Quantity: 2/(Exception e) {e.printstacktrace (
); /** * Collection Processing method * @param map Data Map Object * @param maxresult Collection Size * @param startdate start time
* @param format Time formatted string * @return/Private list<user> getlist (map<string, integer> Map,integer Max
Result,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; The/** * List collection is stored to the map object with the time character key value * @param list collection * @param format time formatted string * @return A Map object with a time character key value */PRIV Ate 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 class:
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;
}
}