Joda-Time ist sehr schön für diese.
compile 'joda-time:joda-time:2.9.9'
String dateTime = "2017-09-21T14:22:28Z";
DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZ");
DateTime myBirthDate = format.parseDateTime(dateTime);
DateTime now = new DateTime();
Period period = new Period(myBirthDate, now);
PeriodFormatter formatter = new PeriodFormatterBuilder()
.appendSeconds().appendSuffix(" Sekonds ago\n")
.appendMinutes().appendSuffix(" Minutes ago\n")
.appendHours().appendSuffix(" Hours ago\n")
.appendDays().appendSuffix(" Days ago\n")
.appendWeeks().appendSuffix(" Weeks ago\n")
.appendMonths().appendSuffix(" Months ago\n")
.appendYears().appendSuffix(" Years ago\n")
.printZeroNever()
.toFormatter();
if (period.getDays()<1) {
String result = formatter.print(period);
System.out.println(result);
} else {
DateTimeFormatter format24hMore = DateTimeFormat.forPattern("yyyy-MM-dd");
String result = format24hMore.print(myBirthDate);
System.out.println(result);
}
EDIT Create-Methode
public String convertDate(String date) {
String result = "";
DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZ");
DateTime from = format.parseDateTime(date);
DateTime now = new DateTime();
Period period = new Period(from, now);
PeriodFormatter formatter = new PeriodFormatterBuilder()
.appendSeconds().appendSuffix(" Sekonds ago\n")
.appendMinutes().appendSuffix(" Minutes ago\n")
.appendHours().appendSuffix(" Hours ago\n")
.appendDays().appendSuffix(" Days ago\n")
.appendWeeks().appendSuffix(" Weeks ago\n")
.appendMonths().appendSuffix(" Months ago\n")
.appendYears().appendSuffix(" Years ago\n")
.printZeroNever()
.toFormatter();
if (period.getDays()<1) {
result = formatter.print(period);
} else {
DateTimeFormatter format24hMore = DateTimeFormat.forPattern("yyyy-MM-dd");
result = format24hMore.print(from);
}
return result;
}
und Sie können diese Methode aufrufen
System.out.println(convertDate("2017-09-21T14:22:28Z"));
Das ist es? Nichts anderes ?? – mani