本帖最后由 lianyangshiyan 于 2009-07-23 10:43:59 编辑

解决方案 »

  1.   

    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Collections;
    import java.util.Date;
    import java.util.LinkedList;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    public class TT {
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    private static LinkedList<Date> dates = new LinkedList<Date>();

     public static void main(String[] args) throws Exception {
    //获取时间
    BufferedReader bReader = new BufferedReader(new FileReader("c:\\tt.txt"));
    String string = bReader.readLine().trim();
    while(string!=null&&!"".equals(string)){
    Pattern pattern = Pattern.compile("\\d{1,2}月\\d{1,2}日");
    Matcher matcher = pattern.matcher(string);
    while(matcher.find()){
    String ss = matcher.group();
    String month = ss.substring(0,ss.indexOf("月"));
    String dd = ss.substring(ss.indexOf("月")+1,ss.indexOf("日"));
    Date dt1 = sdf.parse("2008"+"-"+month+"-"+dd);
    if(!dates.contains(dt1)){
    dates.add(dt1);
    }
    }
    string = bReader.readLine();
    }
    bReader.close();
    //排序
    Collections.sort(dates);
    for(Date date: dates){
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    System.out.println((cal.get(Calendar.MONTH)+1)+"月"+cal.get(Calendar.DATE)+"日");
    }
    //输出最大相差天数
    System.out.println(getDoubleMargin(dates.getLast(),dates.getFirst()));;
    }
     /**
      * 获取相差天数
      * @return
      */
     public static double getDoubleMargin(Date date1, Date date2) {
    double margin;
    try {
    long l = date1.getTime() - date2.getTime();
    margin = (l / (24 * 60 * 60 * 1000.00));
    return margin;
    } catch (Exception e) {
    return 0;
    }
    }
    }
    tt.txt中的数据:
    8月5日 
    6月7日-9月10日 
    3月14日 
    8月1日-8月23日 
    8月24日-8月23日
    8月1日运行结果:3月14日
    6月7日
    8月1日
    8月5日
    8月23日
    8月24日
    9月10日
    180.0