1public static String getAge(String birthdate) throws ParseException {
2 LocalDate parsed = LocalDate.parse("1970-01-01");
3 LocalDate current = LocalDate.now();
4
5 Period p = Period.between(parsed, current);
6
7 // Returns time between date and now
8 return p.getYears() + " Years, " + p.getMonths() + " Months, " + p.getDays() + " Days";
9 }
1import java.util.Date;
2import java.text.SimpleDateFormat;
3class Example{
4 public static void main(String args[]){
5 SimpleDateFormat myFormat = new SimpleDateFormat("dd MM yyyy");
6 String dateBeforeString = "31 01 2014";
7 String dateAfterString = "02 02 2014";
8
9 try {
10 Date dateBefore = myFormat.parse(dateBeforeString);
11 Date dateAfter = myFormat.parse(dateAfterString);
12 long difference = dateAfter.getTime() - dateBefore.getTime();
13 float daysBetween = (difference / (1000*60*60*24));
14 /* You can also convert the milliseconds to days using this method
15 * float daysBetween =
16 * TimeUnit.DAYS.convert(difference, TimeUnit.MILLISECONDS)
17 */
18 System.out.println("Number of Days between dates: "+daysBetween);
19 } catch (Exception e) {
20 e.printStackTrace();
21 }
22 }
23}