java8 日期时间
介绍
常用时间处理
public void test(){
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
// 获取当前日期
LocalDate now = LocalDate.now(); //2021-05-06
// 日期转为String
String today = now.format(dateTimeFormatter); //2021-05-06
// String 转为日期
String date = "2020-01-01";
LocalDate parse = LocalDate.parse(date, dateTimeFormatter);
// 时间加减
LocalDate localDate = now.plusMonths(3); //2021-08-06
LocalDate localDate1 = now.minusMonths(3); //2021-02-06
// 一周前的今天
System.out.println(now.minusWeeks(1));
System.out.println(now.minus(1, ChronoUnit.WEEKS));
// 计算两日期的天数和月数
String date1 = "1955-02-24";
String date2 = "2011-10-05";
LocalDate birthday = LocalDate.parse(date1);
LocalDate death = LocalDate.parse(date2);
Period between = Period.between(birthday, death);
System.out.println(between.getDays());
System.out.println(between.getYears()+"年"+between.getMonths()+"月"+between.getDays()+"天");//56年7月11天
// 是否闰年
System.out.println(now.isLeapYear());
}