import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
//请从控制台接收两个生日,程序能够识别,并打印年龄较大的生日,以及比另一个生日大了多少天。
public class Demo {
public static void main(String[] args) throws ParseException {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个生日:");
String birthday1 = sc.next();
System.out.println("请输入第两个生日:");
String birthday2 = sc.next();
String a = "^\\d{4}-\\d{2}-\\d{2}";
if(birthday1.matches(a) && birthday1.matches(a)){
String s1 = "yyyy-MM-dd";
SimpleDateFormat sdf1 = new SimpleDateFormat(s1);
SimpleDateFormat sdf2 = new SimpleDateFormat(s1);
Date date1 = sdf1.parse(birthday1);
Date date2 = sdf1.parse(birthday2);
long time1 = date1.getTime();
long time2 = date2.getTime();
long sum = time1 - time2;
long day = sum / 1000 / 60 / 60 / 24;
if(day > 0){
System.out.println("大了" + day + "天");
}else{
System.out.println("大了" + (-1 * day) + "天");
}
}
}
}
输入两个生日并返回较大的和大了多少天
最新推荐文章于 2024-09-10 10:21:38 发布