I am formatting my date as follows:
String inputDateString = aMessage.getString("updated_at");
DateTimeFormatter fmt = ISODateTimeFormat.dateTimeNoMillis();
DateTime date = fmt.parseDateTime(inputDateString);
DateTime now = new DateTime();
Period period = new Period(date, now);
PeriodFormatter formatter = new PeriodFormatterBuilder()
.appendSeconds().appendSuffix(" seconds ago\n")
.printZeroNever()
.toFormatter();
String elapsed = formatter.print(period);
dateTextView.setText(elapsed);
I would like to be able to show:
3 seconds ago if the period is less than 60 seconds
3 minutes ago if the period is less than 60 minutes
3 hours ago is the period is less than X hours
3 days ago if the period is less than X days
etc. etc.
How can I achieve this?
Have a look at the methods available on Period, in particular those such as
toStandardSeconds(). Using these it should be straightforward to achieve what you’re after with a fewif-elsestatements making tests such asI think the
PeriodFormatteris a red herring in this case, as I don’t think it’s possible to construct a single formatter that does what you want – and in theifblocks you can just get the seconds/hours/etc. directly using the same method calls as in the conditionals.