又是关于日期的问题 本帖最后由 lianyangshiyan 于 2009-07-23 10:43:59 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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 Jpanel 画图多出了个按钮 localsocket连接提示connect refused,求大侠如何解决 关于java代理模式,代理类为什么要用接口而不用继承? java和C#哪个好? [不是高手,勿入】高分求救。notify 与 notifyAll 什么区别?? 我用快速排序法给随机数排序,为什么有的数小的却排在大的后面呢?例如c[13],c[14] JFrame 刷新问题 初学java,一个关于import的问题 !!!请问刚开始学j2ee的,看什么书好入门??? 我是做Java的,我遇到了两个数学问题 java 引用包太多,linux下进程名太长 JScrollBar的问题。
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