1LocalDate start = LocalDate.parse("2020-11-03");
2LocalDate end = LocalDate.parse("2020-12-15");
3
4long diff = DAYS.between(start, end);
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.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}