How to find the difference in two dates in an Android application

Possible duplicate:
Calculate date and time difference in java

I am new to Android and I want to make a new application for me. I want to tweak two date formats (dd / mm / yyyy) with currentDate. For example, I want to find the differences between the days between 01/02/2013 and currentDate. How can i do this?

+6
source share
2 answers
SimpleDateFormat dfDate = new SimpleDateFormat("dd/MM/yyyy"); java.util.Date d = null; java.util.Date d1 = null; Calendar cal = Calendar.getInstance(); try { d = dfDate.parse("01/02/2012 "); d1 = dfDate.parse(dfDate.format(cal.getTime()));//Returns 15/10/2012 } catch (java.text.ParseException e) { e.printStackTrace(); } int diffInDays = (int) ((d.getTime() - d1.getTime())/ (1000 * 60 * 60 * 24)); System.out.println(diffInDays); 

See Ex1 and Ex2 for a more detailed example.

+19
source

You can take a look at DateUtils , which offers some utility methods for calculating the elapsed time between two dates, but only for presentation. Do not use the direct approach for converting between days / milliseconds, since it does not take into account leap years, leap seconds, summer / winter time changes, etc. These considerations are handled by the Calendar classes.

Update You can use the Joda library to do relative date and time calculations, like the one you need.

+1
source

Source: https://habr.com/ru/post/927723/


All Articles