-1

I have one date. wanna compare it with current date and wants the results in time ago.

Date d1 = new SimpleDateFormat("yyyy-M-dd").parse((String) request.
            getParameter(date1));
Date d2 = new SimpleDateFormat("yyyy-M-dd").parse((String) request.
            getParameter(date2));

long diff = d2.getTime() - d1.getTime();

and want this difference in time ago format.

eg. 3 hours 2 mins ago

Tanu Garg
  • 3,007
  • 4
  • 21
  • 29

7 Answers7

2

If using another library is possible for you, I would recommend Joda Time, especially that JSR 310 it will be introduced in the JDK 8 as the new Date API, being Joda time re arhitected.

Time difference with Joda Time here.

Olimpiu POP
  • 5,001
  • 4
  • 34
  • 49
  • 1
    Actually, [JSR 310: Date and Time API](http://jcp.org/en/jsr/detail?id=310) is included with Java 8. Inspired by Joda-Time, but re-architected. – Basil Bourque Nov 22 '13 at 09:18
1

If you just want to use core Java and not pollute your code with numeric literals:

long diffInMillis = d2.getTime() - d1.getTime();
long diffInHours = TimeUnit.MILLISECONDS.toHours(diffInMillis);
long diffInMins = TimeUnit.MILLISECONDS.toMinutes(diffInMillis);
System.out.printf("%d hours %d mins ago%n", diffInHours, (diffInMins - TimeUnit.HOURS.toMinutes(diffInHours)));
JamesB
  • 7,774
  • 2
  • 22
  • 21
0

You have long value which are milli seconds, so

int min= (int) ((diff / (1000*60)) % 60);
int sec= (int) (diff / 1000) % 60 ;
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
0

Try,

 long diff = d2.getTime() - d1.getTime();
 long m= diff/(60*1000);
 long hour=m/60;
 long minutes=m%60;

 System.out.printf("%d hours %d mins ago%n", hour, minutes)
Masudul
  • 21,823
  • 5
  • 43
  • 58
0

Following is another way to get as per your requirement but getHours() and getMinutes() is deprecated simply do System.out.println((d2.getHours()-d1.getHours())+" hours " +(d2.getMinutes()-d2.getMinutes()) +" mins ago");

SpringLearner
  • 13,738
  • 20
  • 78
  • 116
0

friends.i think we can do this,my code:

public static String getIntervalUpdateTime(long intervalTime) {
    StringBuilder result = new StringBuilder();
    long interval = intervalTime / 1000;
    final long day = 24 * 60 * 60;
    final long hour = 60 * 60;
    final long minute = 60;
    int detailDay = 0;
    int detailHour = 0;
    int detailMinute = 0;
    int detailSecond = 0;
    if (interval >= day) {
        detailDay = (int) (interval / day);
        interval = interval - detailDay * day;
    }
    if (interval >= hour) {
        detailHour = (int) (interval / hour);
        interval = interval - hour * detailHour;
    }
    if (interval >= minute) {
        detailMinute = (int) (interval / minute);
        interval = interval - detailMinute * minute;
    }
    if (interval > 0) {
        detailSecond = (int) interval;
    }
    result.setLength(0);
    if (detailDay > 0) {
        result.append(detailDay);
        result.append("day");
    }
    if (detailHour > 0) {
        result.append(detailHour);
        result.append("hour");
    }
    if (detailMinute > 0) {
        result.append(detailMinute);
        result.append("minutes");
    }
    if (detailSecond > 0) {
        result.append(detailSecond);
        result.append("seconds");
    }
    return result.toString();
}

this is my answer.

PoWen
  • 61
  • 2
0

Use the Period class in Joda-Time 2.3, built just for this purpose.

See example code in answers (especially this one) to the question, How to calculate elapsed time from now with Joda-Time?.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154