revTime - Page 1
Calculating the time elapsed since a specific date.

The display of time elapsed since the date of January 14, 2011, to be clear and easily readable by the user, must be in the form: xx years, xx months, xx days hh: mm: ss, where xx is the corresponding numbers for each data. These numbers xx are the result of a calculation of dates that breaks the period since January 14, 2011 at 5:30 PM until the display is done in real time.

 
Solution using Java class java.util.Date.

A first way that could lead to the solution is to use the java.util.Date Java class. The operation of this class is based on an elapsed time counter since January 1, 1970. Our goal is the calculation of a period between two dates, it would suffice to calculate the difference between the values ​​of this counter to these specific times.

Date referenceDate; //Jan. 14, 2011 5:30:00 PM
Date currentDate;
.
.
yearsNumber = (currentDate.getTime() - referenceDate.getTime()) / MILLISECONDS_PER_YEAR;

Of course, we should then extend the same reasoning to calculate the months, with the rest of what was used for the calculation of years, then the number of days in the same way. The calculation also requires the use of constants, as MILLISECONDS_PER_YEAR, which contain the years, months and days in milliseconds.

The problem with this approach is that it ignores the irregular nature of the dates and schedule. The use of the constant MILLISECONDS_PER_YEAR for example is not always relevant. This number depends on whether a leap year or not. This is also true for the months that do not all have the same number of days. In addition there is also the problem of the summer schedule and winter. All this makes this method of calculating dates unusable. This also explains the deprecated status of the majority of methods in this class since Java 5.

 
Better calculate the dates with the java.util.Calendar class.

The java.util.Calendar class provides insight into the calculations of dates and times as it takes into account all the irregularities that were mentioned above. The treatment principle is different from that used with the Date class. This class works as a calendar where you can browse from year to year, month by month and day by day. The principle for this treatment is initially positioned on the reference date (1-14-2011 5:30:00 p.m. Tunisian local time) and move forward, then, by counting, as and when, years, the months and days.