import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;
public class Dateofbirth {
public static void main(String[] args) throws ParseException {
// 获取当前时间的年月日
Calendar calendar=Calendar.getInstance();
Date date=calendar.getTime();
int YEAR=calendar.get(calendar.YEAR);
int MONDAY=calendar.get(calendar.MONDAY)+1;
int OF_MONTH=calendar.get(calendar.DAY_OF_MONTH);
// 输入出生年月日
System.out.println("请输入你的出生年月日格式为:年/月/日");
Scanner dt=new Scanner(System.in);
String sc=dt.next();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd");
Date date1=sdf.parse(sc);
// 获取输入的出生年月日的年月日
Calendar calendar1=Calendar.getInstance();
calendar1.setTime(date1);
int YEAR1=calendar1.get(calendar1.YEAR);
int MONDAY1=calendar1.get(calendar1.MONDAY)+1;
int OF_MONTH1=calendar1.get(calendar1.DAY_OF_MONTH);
int a=YEAR-YEAR1;
// 判断出生月份与日期
if (MONDAY>MONDAY1){
// System.out.println(1);
System.out.println(a);
}else if(MONDAY<MONDAY1){
// System.out.println(2);
System.out.println(a-1);
}else {
// System.out.println(3);
if (OF_MONTH >= OF_MONTH1) {
System.out.println(a);
}else if(OF_MONTH<OF_MONTH1){
System.out.println(a-1);
}
}
}
}
用户按照格式输入出生年、月、日、计算他几岁了
最新推荐文章于 2024-08-30 18:40:43 发布