1. 请使用日期时间相关的API,计算出一个人已经出生了多少年,再转换为多少天(一年365天)。
public class Test {
public static void main(String[] args) throws ParseException {
// 1. 请使用日期时间相关的API,计算出一个人已经出生了多少年,再转换为多少天(一年365天)。
Scanner sc=new Scanner(System.in);
SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
System.out.println("请输入出生日期 2018-10-13");
String birthday=sc.next();
DateTimeFormatter dtf=DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate localDate=LocalDate.parse(birthday,dtf);
System.out.println(localDate);
Date date1=sdf.parse(birthday);
Date now = new Date(System.currentTimeMillis());
int day=(int)((now.getTime()-date1.getTime())/1000/3600/24);
int year=day/365;
System.out.println(day);
System.out.println(year);
}
}
2. 获取当前的日期,并把这个日期转换为指定格式的字符串
如2088-08-08 08:08:08
public class Test {
public static void main(String[] args) throws ParseException {
LocalDateTime localDate=LocalDateTime.now();
DateTimeFormatter def=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String str1=localDate.format(def);
System.out.println(str1);
}
}
3. 判断2018年2月14日是星期几
public class Test {
public static void main(String[] args) throws ParseException {
LocalDate localDate=LocalDate.of(2018,2,14);
int day=localDate.get(ChronoField.DAY_OF_WEEK);
System.out.println(day);
}
}