1. 请使用日期时间相关的API,计算出一个人已经出生了多少年,再转换为多少天(一年365天)。
Scanner scan = new Scanner(System.in);
System.out.println("请输入您的生日:(2000-12-12)");
String birthdayStr = scan.next();
SimpleDateFormat sdf = new SimpleDateFormat("y-M-d");
Date birthday = sdf.parse(birthdayStr);
Calendar c = Calendar.getInstance();
c.setTime(birthday);
int year = c.get(Calendar.YEAR);
Calendar c2 = Calendar.getInstance();
int year2 = c2.get(Calendar.YEAR);
Date now = new Date();
System.out.println("您出生了"+(year2 - year) + "年");
int days = (int)((now.getTime()-birthday.getTime())/(1000 * 60 * 60 *24));
System.out.println("您出生了"+days + "天");
2. 获取当前的日期,并把这个日期转换为指定格式的字符串
如2088-08-08 08:08:08
Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("y-M-d H:m:s");
String format = sdf.format(now);
System.out.println(format);
LocalDateTime time = LocalDateTime.now();
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("y-M-d H:m:s");
String timeStr = time.format(dtf);
System.out.println(timeStr);
3. 判断2018年2月14日是星期几
Calendar c = Calendar.getInstance();
c.set(2018,2,14);
int week1 = c.get(Calendar.DAY_OF_WEEK);//周日1 周三4
System.out.println(week1);
LocalDate date = LocalDate.of(2018,2,14);
int week2 = date.get(ChronoField.DAY_OF_WEEK);
System.out.println(week2);