Esempio n. 1
0
  /**
   * 返回one和two之间周的差距,以one为基准,每七天+1
   *
   * @param one
   * @param two
   * @return
   */
  public static Long getDiffWeeks(Date one, Date two) {
    if (one.after(two)) {
      return getDiffWeeks(two, one);
    }

    LocalDate startDate = one.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    LocalDate endDate = two.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

    return ChronoUnit.WEEKS.between(startDate, endDate);
  }
  public static void main(String[] args) {
    LocalDateTime date1 = LocalDateTime.of(LocalDate.of(1999, 5, 15), LocalTime.of(10, 22));
    LocalDateTime date2 = LocalDateTime.of(LocalDate.of(2008, 2, 11), LocalTime.of(5, 33));
    System.out.println("date1: " + date1);
    System.out.println("date2: " + date2);

    System.out.println(ChronoUnit.YEARS.between(date1, date2));
    System.out.println(ChronoUnit.MONTHS.between(date1, date2));
    System.out.println(ChronoUnit.WEEKS.between(date1, date2));
    System.out.println(ChronoUnit.DAYS.between(date1, date2));
    System.out.println(ChronoUnit.HOURS.between(date1, date2));
    System.out.println(ChronoUnit.MINUTES.between(date1, date2));
    System.out.println(ChronoUnit.SECONDS.between(date1, date2));
  }