java8 日期类库基本使用
public static void main(String[] args) { /** * java 比较两个日期的差 年 月 日 等 */ DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); LocalDate start_ = LocalDate.parse("1994-09-03", dateTimeFormatter); LocalDateTime end_ = LocalDateTime.now(); long years = ChronoUnit.YEARS.between(start_, end_); long days = ChronoUnit.DAYS.between(start_, end_); System.out.println("年:" + years); System.out.println("天:" + days); //plus LocalDateTime nextWeek = end_.plus(1L, ChronoUnit.WEEKS);//月 年 同理 System.out.println("一周后:" + nextWeek); //minus LocalDateTime preWeek = end_.minusWeeks(1); System.out.println("上一周:" + preWeek); LocalDateTime preDay = end_.minusDays(2); System.out.println("前两天:" + preDay); }