闲话不多说,直接上代码。
以前,我喜欢用这个
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
System.out.println(df.format(new Date()));// new Date()为获取当前系统时间
现在,我用这个
public static void main(String [] arg0){
System.out.println("请输入你的出生日期:格式为20180102 \n");
Scanner scanner = new Scanner(System.in);
//获取当前日期
LocalDate currentDate = LocalDate.now(); // 2018-12-10
System.out.println("当前的日期:" + currentDate);
String birthday = scanner.nextLine();
LocalDate birthDate = LocalDate.parse(birthday,DateTimeFormatter.ofPattern("yyyyMMdd"));
long days = birthDate.until(currentDate, ChronoUnit.DAYS);
System.out.println("你已经活了:" + days + "天 ");
// 获取当前时间
LocalTime cuttentTime = LocalTime.now();//09:56:55
System.out.println("当前的时间:" + cuttentTime);
//获取当前的日期和时间
LocalDateTime curreantDateTime = LocalDateTime.now();
System.out.println("当前的日期和时间:" + curreantDateTime);
//格式化日期时间
LocalDate localDate = LocalDate.parse("2018/12/10",
DateTimeFormatter.ofPattern("yyyy/MM/dd"));
System.out.println("格式化日期:" + localDate);
// 获取本月的最后一天
LocalDate lastDaaofMonth = currentDate.with(TemporalAdjusters.lastDayOfMonth());
System.out.println("本月的最后一天:" + lastDaaofMonth);
//获取2018年最后一个星期五
LocalDate lastMonthlastFriday =currentDate.with(
TemporalAdjusters.lastInMonth(DayOfWeek.FRIDAY));
System.out.println("2018年最后一个星期五:" + lastMonthlastFriday);
//获取昨天的日期
LocalDate yesterday = LocalDate.now().minusDays(-1);
System.out.println("昨天的日期:" + yesterday);
//获取明天的日期
LocalDate tomorrow = LocalDate.now().minusDays(1);
System.out.println("明天的日期:" + tomorrow);
}