计算出两个日期相隔多少天,直接上代码:
import java.text.SimpleDateFormat;
import java.util.Date;
import org.junit.Test;
public class T1 {
@Test
public void t(){
String before = "2013-5-15";
String now = "2013-5-17";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
Date d1 = sdf.parse(before);
Date d2 = sdf.parse(now);
long daysBetween = (d2.getTime() - d1.getTime() + 1000000)
/ (3600 * 24 * 1000);
System.out.println("相隔:"+daysBetween+"天");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
交流探讨到我的新浪微博:http://weibo.com/tianrui1990