1SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
2
3Date birth = sdf.parse("2000-01-01");
4Date now = new Date(System.currentTimeMillis());
5
6Calendar c = Calendar.getInstance();
7c.setTimeInMillis(now.getTime() - birth.getTime());
8int y = c.get(Calendar.YEAR)-1970;
9int m = c.get(Calendar.MONTH);
10int d = c.get(Calendar.DAY_OF_MONTH)-1;
1LocalDate jamesBirthDay = new LocalDate(1955, 5, 19);
2LocalDate now = new LocalDate(2015, 7, 30);
3
4int monthsBetween = Months.monthsBetween(jamesBirthDay, now).getMonths();
5int yearsBetween = Years.yearsBetween(jamesBirthDay, now).getYears();
6
7System.out.println("Month since father of Java born : "
8 + monthsBetween);
9System.out.println("Number of years since father of Java born : "
10 + yearsBetween);
1import java.time.*;
2import java.util.*;
3
4public class Exercise1 {
5 public static void main(String[] args)
6 {
7 LocalDate pdate = LocalDate.of(2012, 01, 01);
8 LocalDate now = LocalDate.now();
9
10 Period diff = Period.between(pdate, now);
11
12 System.out.printf("\nDifference is %d years, %d months and %d days old\n\n",
13 diff.getYears(), diff.getMonths(), diff.getDays());
14 }
15}
16
17