2 qq 21194023 qq_21194023 于 2016.01.15 08:47 提问

Java 从多个日期中找出最大最小时间 50C

1 a 2013-04-01
1 a 2013-04-08
1 a 2013-04-28
2 a 2013-04-08
2 a 2013-11-11
求代码,实现以前两列为唯一,比较多个日期从中找出最早最晚时间,并统计出现次数
1 a 2013-04-01 2013-04-28 3
2 a 2013-04-08 2013-11-11 2

11个回答

u012505618
u012505618   2016.01.15 09:42

lz我不知道你的前两列唯一是什么意思
实现的是对日期的排序统计
具体的lz你在你的需求你基础上改吧

 import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/**
 * Created by Administrator on 2016/1/15.
 */
public class DataComp {
    public static void main(String []arg0){
        String []dateArray = {"2013-04-01","2013-04-08","2013-04-28",
            "2013-04-08", "2013-11-11"};
        showResult(dateArray);
    }

    private static void showResult(String[] dateArray) {
        Map<String, Integer> dateMap = new TreeMap<String, Integer>();
        int i, arrayLen;
        arrayLen = dateArray.length;
        for(i = 0; i < arrayLen; i++){
            String dateKey = dateArray[i];
            if(dateMap.containsKey(dateKey)){
                int value = dateMap.get(dateKey) + 1;
                dateMap.put(dateKey, value);
            }else{
                dateMap.put(dateKey, 1);
            }
        }
        Set<String> keySet = dateMap.keySet();
        String []sorttedArray = new String[keySet.size()];
        Iterator<String> iter = keySet.iterator();
        int index = 0;
        while (iter.hasNext()) {
            String key = iter.next();
        //    System.out.println(key + ":" + dateMap.get(key));
            sorttedArray[index++] = key;
        }
        int sorttedArrayLen = sorttedArray.length;
        System.out.println("最小日期是:" + sorttedArray[0] + "," +
                " 天数为" + dateMap.get(sorttedArray[0]));
        System.out.println("最大日期是:" + sorttedArray[sorttedArrayLen - 1] + "," +
                " 天数为" + dateMap.get(sorttedArray[sorttedArrayLen - 1]));
    }
qq_21194023
qq_21194023 说面上面是原始数据:1 a 2013-04-01 1 a 2013-04-08 1 a 2013-04-28 2 a 2013-04-08 2 a 2013-11-11;说面下面是结果数据:1 a 2013-04-01 2013-04-28 3 2 a 2013-04-08 2013-11-11 2
接近 2 年之前 回复
qq_21194023
qq_21194023 您好 ,理解错误啦,我是想统计1 a 后面的时间 最早出现时间,最晚出现时间,1 a 出现的次数 ‘1 a’是一个整体
接近 2 年之前 回复
enpterexpress
enpterexpress   2016.01.15 09:38

源数据如下

源数据

搜索语句及结果如下

搜索数据

可以查看我的博客SqlServer 根据字段分类汇总信息

yuanwang886
yuanwang886   2016.01.16 11:03
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Test {

    List<Bean> beanList = new ArrayList<Bean>();

    Map<String, Result> map = new HashMap<String, Result>();

    public void setBean(Bean bean) {
        this.beanList.add(bean);
    }

    public void compare() {
        for (Bean bean : beanList) {

            if (map.containsKey(bean.id + bean.letter)) {
                Result result = map.get(bean.id + bean.letter);
                result.compare(bean);
            } else {
                Result result = new Result(bean);
                map.put(bean.id + bean.letter, result);
            }
        }
    }

    public void printResult() {
        for (Map.Entry<String, Result> entry : map.entrySet()) {
            entry.getValue().printResult();
        }
    }

    public static void main(String[] args) {

        Bean a = new Bean("1", "a", "2013-04-01");
        Bean b = new Bean("1", "a", "2013-04-08");
        Bean c = new Bean("1", "a", "2013-04-28");
        Bean d = new Bean("2", "a", "2013-04-08");
        Bean e = new Bean("2", "a", "2013-11-11");
        Bean f = new Bean("1", "a", "2013-11-11");

        Test test = new Test();
        test.setBean(a);
        test.setBean(b);
        test.setBean(c);
        test.setBean(d);
        test.setBean(e);
        test.setBean(f);

        test.compare();
        test.printResult();
    }
}

class Bean {
    String id;
    String letter;
    String date;

    public Bean(String a, String b, String c) {
        this.id = a;
        this.letter = b;
        this.date = c;
    }
}

class Result {
    String id;
    String letter;
    String min;
    String max;
    int num;

    public Result(Bean bean) {
        this.id = bean.id;
        this.letter = bean.letter;
        this.min = bean.date;
        this.max = bean.date;
        this.num = 1;
    }

    public void compare(Bean bean) {
        if (this.min.compareTo(bean.date) > 0) {
            this.min = bean.date;
        } else {
            this.max = bean.date;
        }
        this.num++;
    }

    public void printResult() {
        System.out.println(this.id + this.letter + "   " + this.min + "   "
                + this.max + "   " + this.num);
    }
}
yuanwang886
yuanwang886   2016.01.16 11:04

卧槽,终于好看了~~~~强迫症~

91program
91program   Ds   Rxr 2016.01.15 08:58

如果字符串的格式如上是固定的,可直接使用字符串大小比较来确定最大最小时间,不用传成时间或者数字。

dcxy0
dcxy0   Ds   Rxr 2016.01.15 09:00
weixin_32769751
weixin_32769751   2016.01.15 09:06

so easy....

wenwen091100304
wenwen091100304   2016.01.15 13:56

package stringTest;

public class DateCompare {

static int minCount =1;
static int maxConut =1;
static String mindate ="";
static String maxdate="";

public static void main(String args[]){
    String dates[] =new String[]{"2013-04-01","2013-04-08","2014-03-28","2013-04-08"};
    mindate=dates[0];
    maxdate=dates[0];

    for(int i=0;i<dates.length;i++){
        if(compare(dates[i], mindate)==-1){
            mindate=dates[i];
            minCount=1;
        }else if(compare(dates[i], mindate)==0){
            minCount+=1;
        }
        if(compare(dates[i], maxdate)==1){
            maxdate=dates[i];
            maxConut=1;
        }else if(compare(dates[i], maxdate)==0){
            maxConut+=1;
        }
    }
    System.out.println("最小日期:"+mindate +"出现次数"+minCount);
    System.out.println("最大日期:"+maxdate+"出现次数"+maxConut);

}
public static int compare(String date1,String date2){

    MDate d1 ,d2;
    d1 = str2date(date1);
    d2 =str2date(date2);
    return d1.compareDate(d2);

}
public static MDate str2date(String date){
    MDate d = new MDate();
    d.year = Integer.parseInt(date.substring(0, 4));
    d.month = Integer.parseInt(date.substring(5, 7));
    d.day = Integer.parseInt(date.substring(8, 10));
    return d;

}

static class MDate{
    int year;

    int month;

    int day;

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return "year:"+year+"month:"+month+"day:"+day;
    }

    public int compareDate(MDate date){
        if(this.year-date.year>0){
            return 1;
        }else if(this.year-date.year<0){
            return -1;
        }else{
            if(this.month-date.month>0){
                return 1;
            }else if(this.month-date.month<0){
                return -1;
            }else{
                if(this.day-date.day>0){
                    return 1;
                }else if(this.day-date.day<0){
                    return -1;
                }
                else{
                    return 0;
                }

            }
        }

    }

}

}

运行结果:
最小日期:2013-04-01出现次数2
最大日期:2014-03-28出现次数1

qq_21194023
qq_21194023 说面上面是原始数据:1 a 2013-04-01 1 a 2013-04-08 1 a 2013-04-28 2 a 2013-04-08 2 a 2013-11-11;说面下面是结果数据:1 a 2013-04-01 2013-04-28 3 2 a 2013-04-08 2013-11-11 2
接近 2 年之前 回复
qq_21194023
qq_21194023 您好 ,理解错误啦,我是想统计1 a 后面的时间 最早出现时间,最晚出现时间,1 a 出现的次数 ‘1 a’是一个整体
接近 2 年之前 回复
qq_21194023
qq_21194023 您好 ,理解错误啦,我是想统计1 a 后面的时间 最早出现时间,最晚出现时间,1 a 出现的次数 ‘1 a’是一个整体
接近 2 年之前 回复
yuanwang886
yuanwang886   2016.01.16 10:23

图片说明

如果是数据库(这里是mysql,表名是统计)
select a.id, MAX(a.date), MIN(a.date), COUNT(1)
from tongji a
group by a.id;

yuanwang886
yuanwang886   2016.01.16 10:59

如果是java代码的话,也写了
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Test {

List<Bean> beanList = new ArrayList<Bean>();

Map<String, Result> map = new HashMap<String, Result>();

public void setBean(Bean bean) {
    this.beanList.add(bean);
}

public void compare() {
    for (Bean bean : beanList) {

        if (map.containsKey(bean.id + bean.letter)) {
            Result result = map.get(bean.id + bean.letter);
            result.compare(bean);
        } else {
            Result result = new Result(bean);
            map.put(bean.id + bean.letter, result);
        }
    }
}

public void printResult() {
    for (Map.Entry<String, Result> entry : map.entrySet()) {
        entry.getValue().printResult();
    }
}

public static void main(String[] args) {

    Bean a = new Bean("1", "a", "2013-04-01");
    Bean b = new Bean("1", "a", "2013-04-08");
    Bean c = new Bean("1", "a", "2013-04-28");
    Bean d = new Bean("2", "a", "2013-04-08");
    Bean e = new Bean("2", "a", "2013-11-11");
    Bean f = new Bean("1", "a", "2013-11-11");

    Test test = new Test();
    test.setBean(a);
    test.setBean(b);
    test.setBean(c);
    test.setBean(d);
    test.setBean(e);
    test.setBean(f);

    test.compare();
    test.printResult();
}

}

class Bean {
String id;
String letter;
String date;

public Bean(String a, String b, String c) {
    this.id = a;
    this.letter = b;
    this.date = c;
}

}

class Result {
String id;
String letter;
String min;
String max;
int num;

public Result(Bean bean) {
    this.id = bean.id;
    this.letter = bean.letter;
    this.min = bean.date;
    this.max = bean.date;
    this.num = 1;
}

public void compare(Bean bean) {
    if (this.min.compareTo(bean.date) > 0) {
        this.min = bean.date;
    } else {
        this.max = bean.date;
    }
    this.num++;
}

public void printResult() {
    System.out.println(this.id + this.letter + "   " + this.min + "   "
            + this.max + "   " + this.num);
}

}

共11条数据 1 尾页
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!