how to find difference between two dates in java code example

Example 1: java find time between two dates

public static String getAge(String birthdate) throws ParseException {
		LocalDate parsed = LocalDate.parse("1970-01-01");
		LocalDate current = LocalDate.now();
		
		Period p = Period.between(parsed, current);

  		// Returns time between date and now
		return p.getYears() + " Years, " + p.getMonths() + " Months, " + p.getDays() + " Days";
	}

Example 2: date difference in minutes java

long diff = d2.getTime() - d1.getTime();//as given

long seconds = TimeUnit.MILLISECONDS.toSeconds(diff);
long minutes = TimeUnit.MILLISECONDS.toMinutes(diff);

Tags:

Java Example