So I'm using Joda time and want it to display only one number (I know how to handle pluralisation in Joda, just a quick example at the end).
Example:
2 years 2 months 2 weeks ago
should just show 2 years ago
2 days 2 hours 2 minutes ago
should just show 2 days ago
I'm looking for some way to short circuit the appends when it gets a non-zero number. I have searched and found examples for other frameworks but we are already using Joda time and I dont want to pull inn other dependencies if it is possible.
The code I have now is the following:
protected final String toRelativeTime(final DateTime dateTime) {
DateTime now = new DateTime();
Period period = new Period(dateTime, now);
PeriodFormatter formatter = new PeriodFormatterBuilder()
.appendYears().appendSuffix(" years")
.appendMonths().appendSuffix(" months")
.appendWeeks().appendSuffix(" weeks")
.appendDays().appendSuffix(" days")
.appendHours().appendSuffix(" hours")
.appendMinutes().appendSuffix(" minutes")
.appendSeconds().appendSuffix(" seconds")
.printZeroNever()
.toFormatter();
return formatter.print(period);
}