//1. 请使用日期时间相关的API,计算出一个人已经出生了多少年,再转换为多少天(一年365天)。
public class Test01 {
public static void main(String[] args) throws ParseException {
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
Scanner sc = new Scanner(System.in);
System.out.println("请输入您的生日(格式xxxx:xx:xx):");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy:MM:dd");
String birthday = sc.next();
//将string转换为date
Date parse = sdf.parse(birthday);
System.out.println(parse);
int birthdayYear = Integer.parseInt(birthday.substring(0, 4));
System.out.println("您出生了"+(year-birthdayYear)+"年");
//获取天数
Date nowTime = new Date();
long milliSecond = nowTime.getTime() - parse.getTime();
int day = (int)(milliSecond/1000/60/60/24);
System.out.println(day);
}
}
//2. 获取当前的日期,并把这个日期转换为指定格式的字符串
// 如2088-08-08 08:08:08
public class Task02 {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
System.out.println(sdf.format(date));
}
}
//3. 判断2018年2月14日是星期几
public class Task03 {
public static void main(String[] args) throws ExportException {
Calendar time = Calendar.getInstance();
time.set(2023,0,4);
SimpleDateFormat sdf = new SimpleDateFormat("E");
//System.out.println(sdf.format(time.getTime()).substring(11));
System.out.println(sdf.format(time.getTime()));
}
}