Share Coding

Tutorials, Problems, Stuffs …

Tag Archives: dateformat

[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

Convert unixtime to local time with TimeZone and DateFormat on JAVA

java.util.Date localdate = new java.util.Date((long)(unixtime)*1000);
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");

// Set the TimeZone
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));

// Change the time in 24-hour format
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy kk:mm:ss");