Share Coding

Tutorials, Problems, Stuffs …

[joda time] Convert timestamp into “Posted 3 minutes ago”

DateTimeZone.setDefault(DateTimeZone.UTC);
DateTime myBirthDate = new DateTime(comment.getTime()*1000);
DateTime now = new DateTime();
Period period = new Period(myBirthDate, now);
Log.d("period",period.toString());

PeriodFormatterBuilder builder = new PeriodFormatterBuilder();
if(period.getYears() != 0) {
	builder.appendYears().appendSuffix(" years ago\n");
} else if(period.getMonths() != 0) {
	builder.appendMonths().appendSuffix(" months ago\n");
} else if(period.getDays() != 0) {
	builder.appendDays().appendSuffix(" days ago\n");
} else if(period.getHours() != 0) {
	builder.appendHours().appendSuffix(" hours ago\n");
} else if(period.getMinutes() != 0) {
	builder.appendMinutes().appendSuffix(" minutes ago\n");
} else if(period.getSeconds() != 0) {
	builder.appendSeconds().appendSuffix(" seconds ago\n");
}
PeriodFormatter formatter = builder.printZeroNever().toFormatter();
String elapsed = formatter.print(period);

TextView txtComment_time = (TextView) comment_list_item.findViewById(R.id.txtComment_time);
txtComment_time.setText(elapsed);
Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: