It’s easy when you know how:
// Parse input.
String timeFromServer = "2018-11-27T09:51:31.832107+00:00";
OffsetDateTime odt = OffsetDateTime.parse(timeFromServer);
// Capture the current moment.
OffsetDateTime now = OffsetDateTime.now(ZoneOffset.UTC)
// Get the difference
Duration difference = Duration.between(odt,now);
// Extract details: Convert to minutes and seconds
long minutes = difference.toMinutes();
difference = difference.minusMinutes(minutes);
long seconds = difference.getSeconds();
// Format into string in desired form
String formattedDiff = String.format("%d min %d seconds", minutes, seconds);
System.out.println(formattedDiff);
When run now we get a large number of minutes since your example string is from last month.
34613 min 38 seconds
The only tricky part is splitting out the minutes and the seconds from the Duration
object. I take out the minutes first, then subtract them so only the seconds (and smaller) are left. From Java 9 Duration
has improved support for getting the individual units of time (see to…Part
methods), but I suppose you can’t use that on Android yet.
When it’s only for calculating the difference, it doesn’t matter which time zone or offset you pass to OffsetDateTime.now
.