Skip to content

Instantly share code, notes, and snippets.

@pedrolaranjeiro
Created July 20, 2016 20:06
Show Gist options
  • Save pedrolaranjeiro/92fe6f3d4dda1551da36dd867edee949 to your computer and use it in GitHub Desktop.
Save pedrolaranjeiro/92fe6f3d4dda1551da36dd867edee949 to your computer and use it in GitHub Desktop.
public String getTimestamp() {
DateTime post = new DateTime(1468973396572);
DateTime now = new DateTime();
Period period = new Period(post, now);
PeriodFormatter formatter;
if(period.getYears() != 0){
formatter = new PeriodFormatterBuilder().appendYears().appendSuffix("Y").printZeroNever().toFormatter();
}else if(period.getMonths() !=0){
formatter = new PeriodFormatterBuilder().appendMonths().appendSuffix("M").printZeroNever().toFormatter();
}else if(period.getWeeks() !=0){
formatter = new PeriodFormatterBuilder().appendWeeks().appendSuffix("w").printZeroNever().toFormatter();
}else if(period.getDays() !=0){
formatter = new PeriodFormatterBuilder().appendDays().appendSuffix("d").printZeroNever().toFormatter();
}else if(period.getHours() !=0){
formatter = new PeriodFormatterBuilder().appendHours().appendSuffix("h").printZeroNever().toFormatter();
}else if(period.getMinutes() !=0){
formatter = new PeriodFormatterBuilder().appendMinutes().appendSuffix("m").printZeroNever().toFormatter();
}else{
formatter = new PeriodFormatterBuilder().appendSeconds().appendSuffix("s").printZeroNever().toFormatter();
}
return formatter.print(period);
}
@SubhrajyotiSen
Copy link

Thank you so much for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment