fork download
  1. import java.time.LocalDate;
  2. import java.time.Period;
  3.  
  4. public class Main {
  5. public static void main(String[] args) {
  6. LocalDate startDate = LocalDate.of(2005, 7, 1);
  7. LocalDate endDate = LocalDate.of(2011, 9, 3);
  8. Period period = startDate.until(endDate);
  9. System.out.println(period);
  10.  
  11. // Custom format
  12. String formatted = String.format("%d years, %d months, %d days", period.getYears(), period.getMonths(),
  13. period.getDays());
  14. System.out.println(formatted);
  15. }
  16. }
Success #stdin #stdout 0.08s 49200KB
stdin
Standard input is empty
stdout
P6Y2M2D
6 years, 2 months, 2 days