Home » Programming » Java » How to calculate time ago and time to go in Java

How to calculate time ago and time to go in Java

This article explains how to find the relative time like time ago and times to go in java. Before entering into the program let me explain the sample usage of this program in real-time projects.

Actually a comment section in a post we need to show this relative time like seconds ago, minutes ago and hours ago for every user comment. An E-commerce platform shows the product offer expires within this duration like only 2 hours to go for this offer.

Like this, there are many circumstances we need to calculate this relative time in an application. Hope this program is useful for that.

How to calculate time ago in Java

Calculating relative time in Java we should have knowledge of Date Object and Milliseconds. Actually, Milliseconds plays a vital role in this program. So let us recall the Milliseconds for a glance now.

1 Minute  = 60000 Milliseconds 
1 hour = 60*(60000) Milliseconds
1 day = 24*(60*60000) Milliseconds
1 Month = 30*(24*(60*60000)) Milliseconds
1 Year = 365*((24*(60*60000))) Milliseconds

Below is the java program to find time ago

public static String timeAgo(Date currentDate, Date pastDate) {

  long milliSecPerMinute = 60 * 1000; //Milliseconds Per Minute
  long milliSecPerHour = milliSecPerMinute * 60; //Milliseconds Per Hour
  long milliSecPerDay = milliSecPerHour * 24; //Milliseconds Per Day
  long milliSecPerMonth = milliSecPerDay * 30; //Milliseconds Per Month
  long milliSecPerYear = milliSecPerDay * 365; //Milliseconds Per Year
  //Difference in Milliseconds between two dates
  long msExpired = currentDate.getTime() - pastDate.getTime();

  //Second or Seconds ago calculation
  if (msExpired < milliSecPerMinute) {
    if (Math.round(msExpired / 1000) == 1) {
      return String.valueOf(Math.round(msExpired / 1000)) + " second ago... ";
    } else {
      return String.valueOf(Math.round(msExpired / 1000) + " seconds ago...");
    }
  }

  //Minute or Minutes ago calculation
  else if (msExpired < milliSecPerHour) {
    if (Math.round(msExpired / milliSecPerMinute) == 1) {
      return String.valueOf(Math.round(msExpired / milliSecPerMinute)) + " minute ago... ";
    } else {
      return String.valueOf(Math.round(msExpired / milliSecPerMinute)) + " minutes ago... ";
    }
  }

  //Hour or Hours ago calculation
  else if (msExpired < milliSecPerDay) {
    if (Math.round(msExpired / milliSecPerHour) == 1) {
      return String.valueOf(Math.round(msExpired / milliSecPerHour)) + " hour ago... ";
    } else {
      return String.valueOf(Math.round(msExpired / milliSecPerHour)) + " hours ago... ";
    }
  }

  //Day or Days ago calculation
  else if (msExpired < milliSecPerMonth) {
    if (Math.round(msExpired / milliSecPerDay) == 1) {
      return String.valueOf(Math.round(msExpired / milliSecPerDay)) + " day ago... ";
    } else {
      return String.valueOf(Math.round(msExpired / milliSecPerDay)) + " days ago... ";
    }
  }
  //Month or Months ago calculation 
  else if (msExpired < milliSecPerYear) {
    if (Math.round(msExpired / milliSecPerMonth) == 1) {
      return String.valueOf(Math.round(msExpired / milliSecPerMonth)) + "  month ago... ";
    } else {
      return String.valueOf(Math.round(msExpired / milliSecPerMonth)) + "  months ago... ";
    }
  }
  //Year or Years ago calculation 
  else {
    if (Math.round(msExpired / milliSecPerYear) == 1) {
      return String.valueOf(Math.round(msExpired / milliSecPerYear)) + " year ago...";
    } else {
      return String.valueOf(Math.round(msExpired / milliSecPerYear)) + " years ago...";
    }
  }

}

Lets see the output to passing some sample values in the above java program.

public static void main(String args[]) {
  SimpleDateFormat dateFormatter = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
  String currentDateString = "09-Sep-2020 20:00:00";
  Date currentDate = dateFormatter.parse(currentDateString);

//Past Date in the difference of seconds
  String pastTimeInSecond = "09-Sep-2020 19:59:59";
  Date pastDate = dateFormatter.parse(pastTimeInSecond);
  System.out.println(timeAgo(currentDate, pastDate));
}

The output of the above program is "1 second ago…"

//Past Date in the time difference of minutes
  String pastTimeInMinute = "09-Sep-2020 19:58:00"; 
  Date pastDate = dateFormatter.parse(pastTimeInMinute );
  System.out.println(timeAgo(currentDate, pastDate));

The Output of the above code is "2 minutes ago…"

//Past Date in the time difference of hours
  String pastTimeInHour = "09-Sep-2020 18:00:00"; 
  Date pastDate = dateFormatter.parse(pastTimeInHour );
  System.out.println(timeAgo(currentDate, pastDate));

Output is "2 hours ago…"

//Past Date in the time difference of days
  String pastTimeInDay = "08-Sep-2020 00:00:00"; 
  Date pastDate = dateFormatter.parse(pastTimeInDay );
  System.out.println(timeAgo(currentDate, pastDate));

The Output for the above program is "1 day ago…"

//Past Date in the time difference of months
  String pastTimeInMonth = "09-Jul-2020 18:00:00"; 
  Date pastDate = dateFormatter.parse(pastTimeInMonth );
  System.out.println(timeAgo(currentDate, pastDate));

Output is "2 months ago…"

//Past Date in the time difference of years
  String pastTimeInYear = "09-Sep-2019 18:00:00"; 
  Date pastDate = dateFormatter.parse(pastTimeInYear );
  System.out.println(timeAgo(currentDate, pastDate));

Output for the above code is "1 year ago…". We have seen the example of time ago with seconds, minutes, hours, days, months, and years.

Now, let's start to see how to find time to go in Java. We need to calculate the time to go in some situations in the project. Let say in an eCommerce project if the product sales end within this time duration then we need to show sales ends with some times to go.

How to calculate time to go in Java

The below program calculates time to go.

public static String timeToGo(Date futureDate, Date currentDate) {

  long milliSecPerMinute = 60 * 1000; //Milliseconds Per Minute
  long milliSecPerHour = milliSecPerMinute * 60; //Milliseconds Per Hour
  long milliSecPerDay = milliSecPerHour * 24; //Milliseconds Per Day
  long milliSecPerMonth = milliSecPerDay * 30; //Milliseconds Per Month
  long milliSecPerYear = milliSecPerDay * 365; //Milliseconds Per Year
  //Difference in Milliseconds between two dates
  long msExpired = futureDate.getTime() - currentDate.getTime();

  if (msExpired > 0) {
    //Second or Seconds to go calculation
    if (msExpired < milliSecPerMinute) {
      if (Math.round(msExpired / 1000) == 1) {
        return String.valueOf(Math.round(msExpired / 1000)) + " second to go... ";
      } else {
        return String.valueOf(Math.round(msExpired / 1000) + " seconds to go...");
      }
    }

    //Minute or Minutes to go calculation
    else if (msExpired < milliSecPerHour) {
      if (Math.round(msExpired / milliSecPerMinute) == 1) {
        return String.valueOf(Math.round(msExpired / milliSecPerMinute)) + " minute to go... ";
      } else {
        return String.valueOf(Math.round(msExpired / milliSecPerMinute)) + " minutes to go... ";
      }
    }

    //Hour or Hours to go calculation
    else if (msExpired < milliSecPerDay) {
      if (Math.round(msExpired / milliSecPerHour) == 1) {
        return String.valueOf(Math.round(msExpired / milliSecPerHour)) + " hour to go... ";
      } else {
        return String.valueOf(Math.round(msExpired / milliSecPerHour)) + " hours to go... ";
      }
    }

    //Day or Days to go calculation
    else if (msExpired < milliSecPerMonth) {
      if (Math.round(msExpired / milliSecPerDay) == 1) {
        return String.valueOf(Math.round(msExpired / milliSecPerDay)) + " day to go... ";
      } else {
        return String.valueOf(Math.round(msExpired / milliSecPerDay)) + " days to go... ";
      }
    }
    //Month or Months to go calculation 
    else if (msExpired < milliSecPerYear) {
      if (Math.round(msExpired / milliSecPerMonth) == 1) {
        return String.valueOf(Math.round(msExpired / milliSecPerMonth)) + "  month to go... ";
      } else {
        return String.valueOf(Math.round(msExpired / milliSecPerMonth)) + "  months to go... ";
      }
    }
    //Year or Years to go calculation 
    else {
      if (Math.round(msExpired / milliSecPerYear) == 1) {
        return String.valueOf(Math.round(msExpired / milliSecPerYear)) + " year to go...";
      } else {
        return String.valueOf(Math.round(msExpired / milliSecPerYear)) + " years to go...";
      }
    }
  } else {
    return "Closed";
  }

}

The only difference between the two programs is the time difference in milliseconds. The time difference is always negative when calculating time ago and it is positive when calculating time to go. So based on this I made small changes to the above program.

Lets start to see the output of the program with some sample inputs.

public static void main(String args[]) {

  SimpleDateFormat dateFormatter = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
  String currentDateString = "09-Sep-2020 20:00:00";
  Date currentDate = dateFormatter.parse(currentDateString);

  String futureTimeInSecond = "09-Sep-2020 20:00:01";
  String futureTimeInMinute = "09-Sep-2020 20:01:00";
  String futureTimeInHour = "09-Sep-2020 21:00:00";
  String futureTimeInDay = "10-Sep-2020 20:00:00";
  String futureTimeInMonth = "10-Oct-2020 20:00:01";
  String futureTimeInYear = "10-Sep-2022 20:00:00";

  Date futureDateSecond = dateFormatter.parse(futureTimeInSecond);
  Date futureDateMinute = dateFormatter.parse(futureTimeInMinute);
  Date futureDateHour = dateFormatter.parse(futureTimeInHour);
  Date futureDateDay = dateFormatter.parse(futureTimeInDay);
  Date futureDateMonth = dateFormatter.parse(futureTimeInMonth);
  Date futureDateYear = dateFormatter.parse(futureTimeInYear);

  System.out.println("Difference in Seconds " + timeToGo(futureDateSecond, currentDate));
  System.out.println("Difference in Minutes " + timeToGo(futureDateMinute, currentDate));
  System.out.println("Difference in Hours " + timeToGo(futureDateHour, currentDate));
  System.out.println("Difference in Days " + timeToGo(futureDateDay, currentDate));
  System.out.println("Difference in Months " + timeToGo(futureDateMonth, currentDate));
  System.out.println("Difference in Years " + timeToGo(futureDateYear, currentDate));
}

The output of the above program is below

Difference in Seconds 1 second to go…
Difference in Minutes 1 minute to go…
Difference in Hours 1 hour to go…
Difference in Days 1 day to go…
Difference in Months 1 month to go…
Difference in Years 2 years to go…

Hope this program solves the relative time calculation time ago and time to go. If any clarification please mention it in the comments section below.

Leave a Reply

Your email address will not be published. Required fields are marked *