package xu.xiao.wu817;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
public class DateTest {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
/*求两个日期之间相隔天数
* "2015-10-17"
* "2015-11-9"
* 1.将字符串转换成日期再转换成毫秒值,
* 2.计算毫秒值
* 3.毫秒值的差转换成天数
*/
//1.
String str1 = "2015-10-17";
String str2 = "2015-10-19";
DateFormat dateFormat = DateFormat.getDateInstance();
//日期格式字符串转成date对象
Date date1 = dateFormat.parse(str1);
Date date2 = dateFormat.parse(str2);
//date对象转成毫秒值
long t1 = date1.getTime();
long t2 = date2.getTime();
int s = (int)(Math.abs(t1-t2));
System.out.println(s);
int days = s/1000/60/60/24;
System.out.println(days);
}
}
|
|