22

I am newbie to java and android. One of my android news app i am displaying the time as "7 August 2014, 8:20 am"

But I need to display it like:

5 mins ago    
1 hour ago    
2 days ago

Found many libraries such us pretty time, joda. But i dont know how to add it to my android app. Even this link https://stackoverflow.com/a/13018647/2020685 show me. But how to pass my date and time into it.

Any simple code to do it.

Thanks

Community
  • 1
  • 1
binu j
  • 399
  • 1
  • 3
  • 18

3 Answers3

49

What you want to display is called as the Relative time display. Android provides methods to display time relative to your current time. You don't have to use any third party library just for this purpose.

You can use

DateUtils.getRelativeTimeSpanString(long time, long now, long minResolution)

Refer docs Here

eg.

DateUtils.getRelativeTimeSpanString(your_time_in_milliseconds, current_ time_in_millisecinds,DateUtils.MINUTE_IN_MILLIS);

UPDATE1:

You can try following to pass your date and get the milliseconds for it.

public static long getDateInMillis(String srcDate) {
    SimpleDateFormat desiredFormat = new SimpleDateFormat(
        "d MMMM yyyy, hh:mm aa");

    long dateInMillis = 0;
    try {
        Date date = desiredFormat.parse(srcDate);
        dateInMillis = date.getTime();
        return dateInMillis;
    } catch (ParseException e) {
        Log.d("Exception while parsing date. " + e.getMessage());
        e.printStackTrace();
    }

    return 0;
    }

Hope it helps you.

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
Ritesh Gune
  • 16,629
  • 6
  • 44
  • 72
  • 1
    This is the simplest way of doing what the OP wanted to do. No extra libraries are needed, no extra code logic is needed, just an import! – overflowingStack Aug 07 '14 at 06:07
  • Fine, but i have the time like this "7 August 2014, 8:20 am" How can i pass it to the "DateUtils.getRelativeTimeSpanString(long time, long now, long minResolution)" – binu j Aug 07 '14 at 06:21
  • you need to convert your current date "7 August 2014, 8:20 am" to milliseconds. – Ritesh Gune Aug 07 '14 at 06:45
  • Tried many ways to convert "7 August 2014, 8:20 am" to milliseconds but failed. Any simple way? Thanks – binu j Aug 07 '14 at 08:20
  • I'm following your answer (DateUtils.getRelativeTimeSpanString(your_time_in_milliseconds, System.currentTimeMillis(), DateUtils.MINUTE_IN_MILLIS)) & all I can see the output was "Jan 18, 1970". Can you guys help? – Srikar Reddy Jan 30 '16 at 17:12
  • @Srikar, make sure the time value you are passing is in milliseconds. – Ritesh Gune Feb 01 '16 at 05:59
  • @RiteshGune Thanks. That was the mistake. My time is in seconds and when I changed it to milliseconds, the issue was resolved. – Srikar Reddy Feb 01 '16 at 15:56
  • ok..glad to help you. – Ritesh Gune Feb 03 '16 at 05:27
  • downvoter care to comment?? – Ritesh Gune Apr 11 '16 at 06:12
  • It not give the time ago for me it simply returning the value as 26 Oct 2020. How can i acheive this my DateUtils.getRelativeTimeSpanString(time, System.currentTimeMillis(),DateUtils.MINUTE_IN_MILLIS); my time = 1603714252000 – sejn Nov 10 '20 at 05:36
6

Create the below logic:

Convert your time to seconds, get the diff ( delta ) from current file :

  1. % it by 3600*365
    • if the result > 0 then display year(s) ago
  2. else % it by 3600 * 30
    • if the result > 0 then display month(s) ago
  3. else % it by 3600 * 7
    • if the result > 0 then display week(s) ago
  4. else % it by 3600
    • if the result > 0 then display day(s) ago
  5. else % it by 3600 / 24
    • if the result > 0 then display hour(s) ago
  6. else % it by 60,
    • if the result > 0 then display minute(s) ago

NOTE: % means mod (modulus operation)

Rudy
  • 7,008
  • 12
  • 50
  • 85
2
Date nowDate=new Date();
Date yourPassDate=//you have a date here.
long now=nowDate.getTime();
long time=yourPassDate.getTime();


final long diff = now - time;  //now you have a date interval representing with mileseconds.
//you can use this diff to do something like:
if (diff <1000*60)//less than one minute
    //...
else if (diff <1000*60*60) //less than 1 hour
    //...    
else if (diff < 1000*60*60*24)//less than one day
    //...

This is what java doc of Date.getTime() said:

 * Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT
 * represented by this <tt>Date</tt> object.
JaskeyLam
  • 15,405
  • 21
  • 114
  • 149