I currently have this code:
public static String getTimeAgo(OffsetDateTime dateArg) {
Instant instantNow = Instant.now();
Instant instantThen = dateArg.toInstant();
Duration comparedDuration = Duration.between(instantThen, instantNow);
LocalDate localDate = dateArg.toLocalDate();
LocalDate localDateToday = LocalDate.now(ZoneId.systemDefault());
Period comparedPeriod = Period.between(localDate, localDateToday);
ZonedDateTime zdt1 = ZonedDateTime.ofInstant(instantThen, ZoneId.systemDefault());
ZonedDateTime zdt2 = ZonedDateTime.ofInstant(instantNow, ZoneId.systemDefault());
Calendar calendarFetched = GregorianCalendar.from(zdt1);
Calendar calendarNow = GregorianCalendar.from(zdt2);
Instant d1i = Instant.ofEpochMilli(calendarFetched.getTimeInMillis());
Instant d2i = Instant.ofEpochMilli(calendarNow.getTimeInMillis());
LocalDateTime startDateWeek = LocalDateTime.ofInstant(d1i, ZoneId.systemDefault());
LocalDateTime endDateWeek = LocalDateTime.ofInstant(d2i, ZoneId.systemDefault());
long weeksCalc = ChronoUnit.WEEKS.between(startDateWeek, endDateWeek);
String s = "";
if(comparedPeriod.getYears() > 0) {
s += comparedPeriod.getYears() + " year" + ((comparedPeriod.getYears() == 1) ? " " : "s ");
}
if(weeksCalc > 4){
s += comparedPeriod.getMonths() + " month" + ((comparedPeriod.getMonths() == 1) ? " " : "s ");
}
else{
s += weeksCalc + " weeks ";
}
if (comparedPeriod.getDays() > 0){
s += comparedPeriod.getDays() + " day" + ((comparedPeriod.getDays() == 1) ? " " : "s ");
}
if (comparedDuration.toHoursPart() > 0) {
s += comparedDuration.toHoursPart() + " hour" + ((comparedDuration.toHoursPart() == 1) ? " " : "s ");
}
else{
if(comparedDuration.toMinutesPart() > 0){
s += comparedDuration.toMinutesPart() + " minute" + ((comparedDuration.toMinutesPart() == 1) ? " " : "s ");
}
else s += comparedDuration.toSecondsPart() + " seconds "; // lol lets just end it here...
}
return s + "ago";
}
The goal is to display "time ago" based off from given OffsetDateTime
argument. This is to show the year, month, weeks, days, hours, minutes, all the way down to seconds, if said time units were not at 0
. But I am having trouble calculating the month as the result always come back as 0
, while having an "s" indicating that it is not truly 0
.
For example, running System.out.println(Utils.getTimeAgo(OffsetDateTime.parse("2020-12-13T15:54:43.971273200-05:00")))
displays 1 year 0 months 1 hour ago
as its output...