I use the Joda library to get the period of time elapsed from the specified timestamp:
public static String getTimePassedSince(Date initialTimestamp){
DateTime initDT = new DateTime(initialTimestamp.getTime());
DateTime now = new DateTime();
Period p = new Period(initDT, now);
PeriodFormatter formatter = new PeriodFormatterBuilder()
.appendYears().appendSuffix(" year, ", " years, ")
.appendMonths().appendSuffix(" month, ", " months, ")
.appendDays().appendSuffix(" day, ", " days, ")
.appendHours().appendSuffix(" hour, ", " hours, ")
.appendMinutes().appendSuffix(" minute, ", " minutes, ")
.appendSeconds().appendSuffix(" second, ", " seconds")
.printZeroNever()
.toFormatter();
return formatter.print(p);
}
The function returns the exact time period strings for the given timestamps. For instance:
3 minutes, 23 seconds
1 hour, 30 minutes, 57 seconds
1 day, 23 hours, 21 minutes, 19 seconds
Is there a way so that I can get an approximate time, not an exact one? For example, if a minute and 30 seconds have passed since the moment initialTimestamp, it returns only 1.5 minutes. Similarly, if an hour and 35 minutes have passed, it returns about 1.5 hoursinstead of 1 hour, 35 minutes, xy seconds.
I know that the returned string can be parsed and processed, but I'm looking for something more complex.