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

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

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

参考:

http://www.cnblogs.com/QQParadise/articles/1501420.html

源数据如下

源数据

搜索语句及结果如下

搜索数据

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

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]));
    }

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

图片说明

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

如果是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);
}

}

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

你可以格式化时间,然后获取所有时间的毫秒数进行比较