用JDK特有的时间类求解
先构建原型 再解析
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
long n= sc.nextLong();
for (long i = 0; i < n; i++) {
String arr[]=sc.next().split("-");
//一个人在2月29日出生则没有18岁生日
if(Integer.parseInt(arr[1])==2&&Integer.parseInt(arr[2])==29){
System.out.println("-1");
continue;
}
LocalDate birthday = LocalDate.of(Integer.parseInt(arr[0]),Integer.parseInt(arr[1]) ,Integer.parseInt(arr[1]));
System.out.println( calculateDaysUntil18(birthday));
}
}
public static long calculateDaysUntil18(LocalDate birthday) {
LocalDate turning18 = birthday.plusYears(18);
return ChronoUnit.DAYS.between(birthday, turning18);
}
}