public static void main(String[] args) throws ParseException {
// 今天的时间
String firstString = "2017-06-19";
// 过年的时间
String secondString = "2018-01-28";
// 由于时间格式转化时容易发生Java.text.ParseException: Unparseable date: ""异常,请写到try块中
try {
// 指定时间类型转换
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
// 把String字符串转成时间类型
Date parse1 = simpleDateFormat.parse(firstString);
Date parse2 = simpleDateFormat.parse(secondString);
// 返回从1970 年 1 月 1 日午夜(GMT 时间)到指定时间的毫秒数
long one = parse1.getTime();
long two = parse2.getTime();
long total = 0;
// 判断两个时间段的大小
if(one>two){
total = one-two;
}else{
total = two-one;
}
// 除24个小时60分钟60秒1000毫秒
total = total/24/60/60/1000;
System.out.println("总天数:"+total);
} catch (Exception e) {
System.out.println("异常:"+e);
}
}
// 今天的时间
String firstString = "2017-06-19";
// 过年的时间
String secondString = "2018-01-28";
// 由于时间格式转化时容易发生Java.text.ParseException: Unparseable date: ""异常,请写到try块中
try {
// 指定时间类型转换
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
// 把String字符串转成时间类型
Date parse1 = simpleDateFormat.parse(firstString);
Date parse2 = simpleDateFormat.parse(secondString);
// 返回从1970 年 1 月 1 日午夜(GMT 时间)到指定时间的毫秒数
long one = parse1.getTime();
long two = parse2.getTime();
long total = 0;
// 判断两个时间段的大小
if(one>two){
total = one-two;
}else{
total = two-one;
}
// 除24个小时60分钟60秒1000毫秒
total = total/24/60/60/1000;
System.out.println("总天数:"+total);
} catch (Exception e) {
System.out.println("异常:"+e);
}
}