0

How we can find last comment time ago in minute or hour or day using java

For example:

8 minutes ago 8 hours ago 8 days ago 8 months ago 8 years ago

Akash Jain
  • 19
  • 4

3 Answers3

2

Add below maven dependency

<dependency>
    <groupId>org.ocpsoft.prettytime</groupId>
    <artifactId>prettytime</artifactId>
    <version>1.0.8.Final</version>
</dependency> 

Here is sample code for

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

import org.ocpsoft.pretty.time.PrettyTime;

    public class TestClass {

        public static void main(String[] args) {        

            LocalDateTime currentTime = LocalDateTime.now();
            Date before5HrDate = Date.from(currentTime.minusHours(5).atZone(ZoneId.systemDefault()).toInstant());

            PrettyTime p = new PrettyTime();
            String is  = p.format(before5HrDate);

            System.out.println("Is : "  + is );
        }

    }

Answer : Is : 5 hours ago

Ravi Kavaiya
  • 829
  • 6
  • 16
0

Just perform a substraction between the two date times and give it some formatting with the SimpleDateFormat class.

Here is a link that might help you https://www.tutorialspoint.com/java/java_date_time.htm

Some random IT boy
  • 7,569
  • 2
  • 21
  • 47
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Apr 06 '18 at 06:21
0

// use this class

public class TimeUtils {

  public final static long ONE_SECOND = 1000;
  public final static long SECONDS = 60;

  public final static long ONE_MINUTE = ONE_SECOND * 60;
  public final static long MINUTES = 60;

  public final static long ONE_HOUR = ONE_MINUTE * 60;
  public final static long HOURS = 24;

  public final static long ONE_DAY = ONE_HOUR * 24;

  private TimeUtils() {
  }

  /**
   * converts time (in milliseconds) to human-readable format
   *  "<w> days, <x> hours, <y> minutes and (z) seconds"
   */
  public static String millisToLongDHMS(long duration) {
    StringBuffer res = new StringBuffer();
    long temp = 0;
    if (duration >= ONE_SECOND) {
      temp = duration / ONE_DAY;
      if (temp > 0) {
        duration -= temp * ONE_DAY;
        res.append(temp).append(" day").append(temp > 1 ? "s" : "")
           .append(duration >= ONE_MINUTE ? ", " : "");
      }

      temp = duration / ONE_HOUR;
      if (temp > 0) {
        duration -= temp * ONE_HOUR;
        res.append(temp).append(" hour").append(temp > 1 ? "s" : "")
           .append(duration >= ONE_MINUTE ? ", " : "");
      }

      temp = duration / ONE_MINUTE;
      if (temp > 0) {
        duration -= temp * ONE_MINUTE;
        res.append(temp).append(" minute").append(temp > 1 ? "s" : "");
      }

      if (!res.toString().equals("") && duration >= ONE_SECOND) {
        res.append(" and ");
      }

      temp = duration / ONE_SECOND;
      if (temp > 0) {
        res.append(temp).append(" second").append(temp > 1 ? "s" : "");
      }
      return res.toString();
    } else {
      return "0 second";
    }
  }


  public static void main(String args[]) {
    System.out.println(millisToLongDHMS(123));
    System.out.println(millisToLongDHMS((5 * ONE_SECOND) + 123));
    System.out.println(millisToLongDHMS(ONE_DAY + ONE_HOUR));
    System.out.println(millisToLongDHMS(ONE_DAY + 2 * ONE_SECOND));
    System.out.println(millisToLongDHMS(ONE_DAY + ONE_HOUR + (2 * ONE_MINUTE)));
    System.out.println(millisToLongDHMS((4 * ONE_DAY) + (3 * ONE_HOUR)
        + (2 * ONE_MINUTE) + ONE_SECOND));
    System.out.println(millisToLongDHMS((5 * ONE_DAY) + (4 * ONE_HOUR)
        + ONE_MINUTE + (23 * ONE_SECOND) + 123));
    System.out.println(millisToLongDHMS(42 * ONE_DAY));
    /*
      output :
            0 second
            5 seconds
            1 day, 1 hour
            1 day and 2 seconds
            1 day, 1 hour, 2 minutes
            4 days, 3 hours, 2 minutes and 1 second
            5 days, 4 hours, 1 minute and 23 seconds
            42 days
     */
}

}