Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Time ago for Android/java [closed]

Tags:

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

like image 871
binu j Avatar asked Aug 07 '14 05:08

binu j


People also ask

How do I see time ago on Android?

In Android you can use DateUtils. getRelativeTimeSpanString(long timeInMillis), referring to https://developer.android.com/reference/android/text/format/DateUtils.html you can use one of the variations of that method for accuracy. Show activity on this post. Show activity on this post.

How can I get current date and time in Android?

Calendar Date currentTime = Calendar. getInstance(). getTime();

Does Java support Android?

Current versions of Android use the latest Java language and its libraries (but not full graphical user interface (GUI) frameworks), not the Apache Harmony Java implementation, that older versions used. Java 8 source code that works in latest version of Android, can be made to work in older versions of Android.

How do I get the current time in Kotlin?

As Kotlin is interoperable with Java, we will be using the Java utility class and Simple Date Format class in order to get the current local date and time.


2 Answers

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.

like image 200
Ritesh Gune Avatar answered Oct 06 '22 13:10

Ritesh Gune


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)

like image 28
Rudy Avatar answered Oct 06 '22 13:10

Rudy