SuperBale
SuperBale

Reputation: 351

Calculate how long has passed Date java

I have a function:

  public String getTimePast(Date d) {
  //insertcodehere
 }

That takes in a Date of a message and must return how much time has past based on the current time in specific format. For example if it has just been posted it will say "Now"

If it has been 4 minutes it will say "4min" If it has been 23hrs it will say "23hrs" Etc

Below is how I tried to do it with no luck! How am I able to do this? Thank you!

public String getTimePast(Date d) {


    Calendar c = Calendar.getInstance();

    int hr = c.get(Calendar.HOUR_OF_DAY);
    int min = c.get(Calendar.MINUTE);
    int day = c.get(Calendar.DAY_OF_MONTH);
    int month = c.get(Calendar.MONTH);
    int year = c.get(Calendar.YEAR);


    if (year == d.getYear()) {
        if (month == d.getMonth()) {
            if (day == d.getDay()) {
                if (hr == d.getHours()) {
                    if (min == d.getMinutes()) {
                        return "Now";
                    } else {
                        return min - d.getMinutes() + "m";
                    }
                } else {
                    return hr - d.getHours() + "hr";

                }
            } else {
                return day - d.getDay() + "d";
            }
        } else {
            return month - d.getMonth() + "m";
        }
    } else {
        return year - d.getYear() + "y";
    }


}

Upvotes: 2

Views: 2971

Answers (3)

Doug Stevenson
Doug Stevenson

Reputation: 317467

The easiest and most correct way to do this in Android is to use one of the functions in DateUtils, such as one of the variants of getRelativeTimeSpanString(). Which one to use is up to your requirements. This should be preferred because it will format the string according to the current locale, so it should work in any language supported by the device.

Upvotes: 0

felipecrp
felipecrp

Reputation: 1069

You can use java.util.concurrent.TimeUnit

long diff = c.getTimeInMillis() - d.getTime();
long minutes = TimeUnit.MILLISECONDS.toMinutes(diff);
long hours = TimeUnit.MILLISECONDS.toHours(diff);
//(...)

and them check for values, like:

if (minutes > 60) {
    if (hours > 24) {
       // print days
    } else {
       // print hours
    }
} else {
    // print minutes
}

Upvotes: 2

Scary Wombat
Scary Wombat

Reputation: 44834

How about using another Calendar Object as simply finding the difference

Calendar now = Calendar.getInstance();
Calendar start = Calendar.getInstance();
start.setTime (d);

long milliseconds1 = start.getTimeInMillis();
  long milliseconds2 = now.getTimeInMillis();
  long diff = milliseconds2 - milliseconds1;
  long diffSeconds = diff / 1000;
  long diffMinutes = diff / (60 * 1000);
  long diffHours = diff / (60 * 60 * 1000);
  long diffDays = diff / (24 * 60 * 60 * 1000);
  System.out.println("\nThe Date Different Example");
  System.out.println("Time in milliseconds: " + diff
 + " milliseconds.");
  System.out.println("Time in seconds: " + diffSeconds
 + " seconds.");
  System.out.println("Time in minutes: " + diffMinutes 
+ " minutes.");
  System.out.println("Time in hours: " + diffHours 
+ " hours.");
  System.out.println("Time in days: " + diffDays 
+ " days.");
  }

see http://www.roseindia.net/java/beginners/DateDifferent.shtml

Upvotes: 5

Related Questions