简体   繁体   English

如何使用Java根据日期将月份分组并计算总数?

[英]How to group by month based on date using java and calculate total count?

How to group by month based on date using java and calculate total count? 如何使用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);
            }
        }
    }
}

I dont know how to modify this map and list.Please give me suggestion its a correct way or not or any other comparator method 我不知道如何修改此地图和列表。请给我建议它的正确方法或其他比较方法

i expected this output 我期望这个输出

# | Month |  Year  | TotalSMSSent
1    Oct     2015   4
2    Oct     2016   1

I have done some modifications to your code hope it will help.I have created a new class records called Records to hold all your records. 我对您的代码做了一些修改,希望对您有所帮助。我创建了一个名为Records的新类记录来保存您的所有记录。

I have assumed that your date are in string form since you are not using Java Date 我假设您的日期是字符串形式,因为您没有使用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;
}

}

The Output Will be : 输出将是:

0   2016    Oct 1
1   2015    Oct 4

modify the code according to your requirement. 根据您的要求修改代码。

Please see below as an idea. 请参阅下面的想法。

 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()); 

The output will be as below (Note, I have not formatted the output, I just print out the Report hashmap). 输出将如下所示(注意,我尚未格式化输出,我只是打印出报告哈希图)。

{Month 1-Year 2015=0, Month 2-Year 2015=0, Month 3-Year 2015=0, Month 4-Year 2015=0, Month 5-Year 2015=0, Month 6-Year 2015=0, Month 7-Year 2015=0, Month 8-Year 2015=0, Month 9-Year 2015=0, Month 10-Year 2015=4 , Month 11-Year 2015=0, Month 12-Year 2015=0, Month 1-Year 2016=0, Month 2-Year 2016=0, Month 3-Year 2016=0, Month 4-Year 2016=0, Month 5-Year 2016=0, Month 6-Year 2016=0, Month 7-Year 2016=0, Month 8-Year 2016=0, Month 9-Year 2016=0, Month 10-Year 2016=1 , Month 11-Year 2016=0, Month 12-Year 2016=0} {第1个月2015年= 0,第2个月2015年= 0,第3个月2015年= 0,第4个月2015年= 0,第5年2015年= 0,第6个月2015年= 0,本月2015年7月= 0、2015年8月8月= 2015年9月9月= 0、 2015年10月10月= 4 、2015年11月11月= 0、2015年12月0月= 0、1月1月年2016 = 0,月2年2016 = 0,月3年2016 = 0,月4年2016 = 0,月5年2016 = 0,月6年2016 = 0,月7年2016 = 0,第8个月(2016年)= 0,第9个月(2016年)= 0,第10个月(2016年)= 1 ,第11个月(2016年)= 0,第12个月(2016年)= 0}

You can customize the code as much as you can achieve your purpose. 您可以自定义代码,以达到自己的目的。 Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java:根据时区计算月份结束日期 - Java: Calculate month end date based on timezone 如何使用java 8在文本文件中查找单词总数、元音总数、特殊字符总数 - How to find total count of Words, total count of Vowels, total count of Special Character in a text file using java 8 如何在 Java 8 中使用 groupby 函数计算计数? - How to calculate count using groupby function in Java 8? Java 8-按列表分组,排序并显示总数 - Java 8 - Group by a List, sort and display the total count of it 如何使用存储自毫秒的Postgres日期起使用Month进行分组 - How to Group By using Month from date stored as millisecond Postgres 使用 Java 8 如何根据过滤条件按列表过滤和分组并转换为 Map 及其计数 - using Java 8 How to filter by list and group by based on filter condition and convert to Map with their count 如何使用Android Studio在Java中计算总数 - How to count sum total in Java using Android Studio 如何使用 java 计算给定字符串的 substring 中的总元音? - How to count the total vowels in substring of a given string using java? 如何从希伯来日历中的总天数差异计算月份 - How To Calculate Month From The Total Number Of Days Difference In Hebrew Calendar Java 8 Date API - 获取一个月内的总周数 - Java 8 Date API - Get total number of weeks in a month
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM