import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class MyCalendar {
public static void main(String[] args) throws ParseException {
// 1.提示用户填写具体时间
// 如输入格式:1998-1-5
Scanner sc = new Scanner(System.in);
System.out.print("请输入第一次决定喜欢她的时间:");
String input = sc.next();
Scanner endSc = new Scanner(System.in);
System.out.print("输入当前日期:");
String endput = endSc.next();
//正则表达式限制年月日的格式,稍微有点长
String regx = "([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8])))";
String endregx = "([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8])))";
boolean re = input.matches(regx);
boolean endre = endput.matches(endregx);
if (re == false && endre == false) {
System.out.println("您输入的日期格式不正确!请重新输入!");
} else {
SimpleDateFormat stf = new SimpleDateFormat("yyyy-MM-dd");
Date sr = stf.parse(input);
Date endsr = stf.parse(endput);
long res1 = sr.getTime();
long res2 = endsr.getTime();
// 因为当前地区处于东八区,多余8小时,因此需要减去8小时的毫秒数,然后除去一天的毫秒数
// 因为当前一天减去的数量没有计算前面一天,因此需要在总数上加1
long res = ((res2 - res1) - 8 * 60 * 1000) / (24 * 60 * 60 * 1000)
+ 1;
System.out.println("距离喜欢她已经" + res + "天了!!");
}
sc.close();
}
}
07-20
07-20
07-20
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交