import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date1;
Date date2;
try {
date1 = sdf.parse("2007-11-26");
date2 = sdf.parse("2013-02-14");
System.out.println(new Test().dateDiff(date1,date2));
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static long dateDiff(Date d1, Date d2) throws Exception {
long n1 = d1.getTime();
long n2 = d2.getTime();
long diff = Math.abs(n1 - n2);
diff /= 3600 * 1000 * 24;
return diff;
}
}
计算2个日期相差天数
最新推荐文章于 2024-08-26 16:40:46 发布