如何使用java根据日期按月分组并计算总数?
public static void main(String[] args) {
Map<String, Object>out=new HashMap<String, Object>();
Map<String,Object> hm=new HashMap<String, Object>();
List<String> al= new ArrayList<String>();
al.add("51b6f5fde4b0dd92df2c3270");
al.add("51b866e9e4b021170dd1ae1c");
hm.put("sDate","02-Oct-2015");
hm.put("status","S");
hm.put("SMSSentId", al);
out.put("Student1", hm);
Map<String,Object> hm1=new HashMap<String, Object>();
List<String> al1= new ArrayList<String>();
al1.add("51b6f5fde4b0dd92df2c3271");
al1.add("51b866e9e4b021170dd1ae12");
hm1.put("sDate","03-Oct-2015");
hm1.put("status","S");
hm1.put("SMSSentId", al1);
out.put("Student2", hm1);
Map<String,Object> hm2=new HashMap<String, Object>();
List<String> al2= new ArrayList<String>();
al2.add("51b6f5fde4b0dd92df2c3271");
hm2.put("sDate","03-Oct-2016");//Year changed
hm2.put("status","S");
hm2.put("SMSSentId", al2);
out.put("Student3", hm2);
//System.out.println(out);
for (Map.Entry<String, Object> entry : out.entrySet())
{
// System.out.println(entry.getKey() + "/" + entry.getValue());
for (Map.Entry<String, Object> entry1 : hm.entrySet())
{
System.out.println(entry1.getKey() + "/" + entry1.getValue());
if(entry1.getKey().equals("SMSSentId"))
{
int a= ((List<String>) entry1.getValue()).size();
System.out.println(a);
}
}
}
}
我不知道如何修改此地图和列表。请给我建议是否正确 或任何其他比较器方法
我期待这个输出
# | Month | Year | TotalSMSSent
1 Oct 2015 4
2 Oct 2016 1
我对您的代码做了一些修改,希望它会有所帮助。我创建了一个名为 Records 的新类记录来保存您的所有记录。
我假设你的日期是字符串形式,因为你没有使用Java
Date
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Sorted {
public static void main(String[] args) {
Map<String, Records> out = new HashMap<String, Records>();
List<String> al = new ArrayList<String>();
al.add("51b6f5fde4b0dd92df2c3270");
al.add("51b866e9e4b021170dd1ae1c");
Records record = new Records("02-Oct-2015", "S", al);
out.put("student1", record);
al = new ArrayList<String>();
al.add("51b6f5fde4b0dd92df2c3271");
al.add("51b866e9e4b021170dd1ae12");
record = new Records("03-Oct-2015", "S", al);
out.put("Student2", record);
al = new ArrayList<String>();
al.add("51b6f5fde4b0dd92df2c3271");
record = new Records("03-Oct-2016", "S", al);
out.put("Student3", record);
process(out);
}
public static void process(Map<String, Records> records) {
HashMap<String, HashMap<String, Integer>> m = new HashMap<String, HashMap<String, Integer>>();
HashMap<String, Integer> month = null;
for (String recordKey : records.keySet()) {
Records r = records.get(recordKey);
String s[] = r.getsDate().split("-");
if (!m.containsKey(s[2])) {
month = new HashMap<String, Integer>();
m.put(s[2], month);
}
HashMap<String, Integer> m1 = m.get(s[2]);
if (!m1.containsKey(s[1])) {
m1.put(s[1], records.get(recordKey).getSMSSent().size());
} else {
int flag = m1.get(s[1]);
m1.put(s[1], flag + records.get(recordKey).getSMSSent().size());
}
}
display(m);
}
public static void display(HashMap<String, HashMap<String, Integer>> d) {
int i = 0;
for (String s : d.keySet()) {
Map<String, Integer> m = d.get(s);
for (String s1 : m.keySet()) {
System.out.print(i++);
System.out.print("\t");
System.out.print(s);
System.out.print("\t");
System.out.print(s1 + "\t" + m.get(s1));
System.out.println("");
}
}
}
}
class Records {
private String sDate;
private String status;
private List<String> SMSSent;
public Records(String sDate, String status, List<String> sMSSent) {
super();
this.sDate = sDate;
this.status = status;
SMSSent = sMSSent;
}
public String getsDate() {
return sDate;
}
public void setsDate(String sDate) {
this.sDate = sDate;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public List<String> getSMSSent() {
return SMSSent;
}
public void setSMSSent(List<String> sMSSent) {
SMSSent = sMSSent;
}
}
输出将是:
0 2016 Oct 1
1 2015 Oct 4
根据您的要求修改代码。
请参阅下面的想法。
final SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy"); Map<String, Object> out=new HashMap<String, Object>(); Map<String,Object> hm=new HashMap<String, Object>(); List<String> al= new ArrayList<String>(); al.add("51b6f5fde4b0dd92df2c3270"); al.add("51b866e9e4b021170dd1ae1c"); hm.put("sDate","02-Oct-2015"); hm.put("status","S"); hm.put("SMSSentId", al); out.put("Student1", hm); Map<String,Object> hm1=new HashMap<String, Object>(); List<String> al1= new ArrayList<String>(); al1.add("51b6f5fde4b0dd92df2c3271"); al1.add("51b866e9e4b021170dd1ae12"); hm1.put("sDate","03-Oct-2015"); hm1.put("status","S"); hm1.put("SMSSentId", al1); out.put("Student2", hm1); Map<String,Object> hm2=new HashMap<String, Object>(); List<String> al2= new ArrayList<String>(); al2.add("51b6f5fde4b0dd92df2c3271"); hm2.put("sDate","03-Oct-2016");//Year changed hm2.put("status","S"); hm2.put("SMSSentId", al2); out.put("Student3", hm2); List<Integer> years = Arrays.asList(2015,2016); List<Integer> months = Arrays.asList(1,2,3,4,5,6,7,8,9,10,11,12); Map<String, Integer> report = new LinkedHashMap<String, Integer>(); for (Integer year : years) { for (Integer month : months) { Integer smsCount = 0; // loop on Students for (Map.Entry<String, Object> outEntry : out.entrySet()) { Map studentData = (Map)outEntry.getValue(); String sentDateAsString = (String)studentData.get("sDate"); Date sentDate = dateFormat.parse(sentDateAsString); Calendar cal = Calendar.getInstance(); cal.setTime(sentDate); if (cal.get(Calendar.MONTH) == (month - 1) && cal.get(Calendar.YEAR) == year) { List smsList = (List)studentData.get("SMSSentId"); smsCount += smsList.size(); } } report.put(String.format("Month %d-Year %d", month, year), smsCount); } } System.out.println(report.toString());
输出如下(注意,我没有格式化输出,我只是打印出报告哈希图)。
{2015年第1月=0、2015年第2月=0、2015年第3月=0、2015年第4月=0、2015年第5月=0、2015年第6月=0, 2015 年第 7 月=0、2015 年第 8 月=0、2015 年第 9 月=0、2015 年第 10 月=4、2015 年第 11 月=0、2015 年第 12 月=0、 2016 年第 1 月=0、2016 年第 2 月=0、2016 年第 3 月=0、2016 年第 4 月=0、2016 年第 5 月=0、2016 年第 6 月=0、7 月-2016年=0,2016年第8月=0,2016年第9月=0,2016年第10月=1,2016年第11月=0,2016年第12月=0}
您可以根据自己的目的自定义代码。希望有帮助。