1String dateStart = "01/14/2015 08:29:58";
2String dateStop = "01/15/2015 11:31:48";
3
4//HH converts hour in 24 hours format (0-23), day calculation
5SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
6
7Date d1 = null;
8Date d2 = null;
9
10d1 = format.parse(dateStart);
11d2 = format.parse(dateStop);
12
13//in milliseconds
14long diff = d2.getTime() - d1.getTime();
15
16long diffSeconds = diff / 1000 % 60;
17long diffMinutes = diff / (60 * 1000) % 60;
18long diffHours = diff / (60 * 60 * 1000) % 24;
19long diffDays = diff / (24 * 60 * 60 * 1000);
20
21System.out.print(diffDays + " days, ");
22System.out.print(diffHours + " hours, ");
23System.out.print(diffMinutes + " minutes, ");
24System.out.print(diffSeconds + " seconds.");
25
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;
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}