Pages

Saturday, June 13, 2015

How to format datetime to RelativeTime from now in android


Hello everyone,

Today I am write post for display Relative Time from now in application.
Some application we need to show Datetime in relative to now. Application like chat, post Artical and showing news headline.


Relative time from now mean, examples below:

10 second ago
1 min ago
5 hours ago
2 days ago


I find out some way to format Datetime to Relative time from now.

1. Using DateUtils API
2. Using android-ago library


1. Using DateUtils API 

First understand below methods and it's parameters

Method 1 : 


public static CharSequence getRelativeTimeSpanString (long time, long now, long minResolution)

Returns a string describing 'time' as a time relative to 'now'.
Time spans in the past are formatted like "42 minutes ago". Time spans in the future are formatted like "in 42 minutes".

Parameters:

time : the time to describe, in milliseconds
now : the current time in milliseconds
minResolution : the minimum timespan to report.
For example, a time 3 seconds in the past will be reported as "0 minutes ago" if this is set to MINUTE_IN_MILLIS. Pass one of 0, MINUTE_IN_MILLIS, HOUR_IN_MILLIS, DAY_IN_MILLIS, WEEK_IN_MILLIS

See below Example code and Output one by one.
If you have Date-time is a string then first convert in Date Object see in code.


Exmaple  :


try {
 long now = System.currentTimeMillis();
 String datetime1 = "06/12/2015 03:58 PM";
 SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm aa");
 Date convertedDate = dateFormat.parse(datetime1);
  
 CharSequence relavetime1 = DateUtils.getRelativeTimeSpanString(
  convertedDate.getTime(),
        now,
        DateUtils.SECOND_IN_MILLIS);
   
 txt_time.append(relavetime1+"\n\n");
 System.out.println(relavetime1);
} catch (ParseException e) {
 e.printStackTrace();
}

If current Time is a "06/12/2015 03:58:10 PM" then output like below because we pass minResolutions parameters is SECOND_IN_MILLIS.


 Output : 
 10 second ago


See below case after change minResolutions parameters


Case 1. MINUTE_IN_MILLIS then output : "0 minutes ago"
Case 2. HOUR_IN_MILLIS then output : "0 hours ago"
Case 3. DAY_IN_MILLIS then output : "Today"
Case 4. WEEK_IN_MILLIS then output : "12 Jun 2015"


Method 2:


public static CharSequence getRelativeTimeSpanString (long time, long now, long minResolution, int flags)

Returns a string describing 'time' as a time relative to 'now'.
Time spans in the past are formatted like "42 minutes ago". Time spans in the future are formatted like "in 42 minutes".
Can use FORMAT_ABBREV_RELATIVE flag to use abbreviated relative times, like "42 mins ago".

Parameters:

time the time to describe, in milliseconds
now : the current time in milliseconds
minResolution : the minimum timespan to report.
For example, a time 3 seconds in the past will be reported as "0 minutes ago" if this is set to MINUTE_IN_MILLIS. Pass one of 0, MINUTE_IN_MILLIS, HOUR_IN_MILLIS, DAY_IN_MILLIS, WEEK_IN_MILLIS
flags a bit mask of formatting options, such as FORMAT_NUMERIC_DATE or FORMAT_ABBREV_RELATIVE


Example code  1 :


try {
 long now = System.currentTimeMillis();
 String datetime1 = "06/12/2015 03:58 PM";
 SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm aa");
 Date convertedDate = dateFormat.parse(datetime1);
  
 CharSequence relavetime1 = DateUtils.getRelativeTimeSpanString(
  convertedDate.getTime(),
        now,
        DateUtils.SECOND_IN_MILLIS,
        DateUtils.FORMAT_ABBREV_RELATIVE);
   
 txt_time.append(relavetime1+"\n\n");
 System.out.println(relavetime1);
} catch (ParseException e) {
 e.printStackTrace();
}

If current Time is a "06/12/2015 03:58:10 PM" then output like below because we pass minResolutions parameters is SECOND_IN_MILLIS.


 Output : 
 10 secs ago


See below case if change minResolutions parameters


Case 1. MINUTE_IN_MILLIS then output : "0 mins ago"
Case 2. HOUR_IN_MILLIS then output : "0 hours ago"
Case 3. DAY_IN_MILLIS then output : "Today"
Case 4. WEEK_IN_MILLIS then output : "12 June"


Example code 2 :
If target datetime is "06/12/2015 06:00 PM" and current time is "06/12/2015 05:15 PM" output shows like below



Case 1. SECOND_IN_MILLIS then output : "in 45 mins" 
Case 2. MINUTE_IN_MILLIS then output : "in 45 mins"
Case 3. HOUR_IN_MILLIS then output : "in 0 hours"
Case 4. DAY_IN_MILLIS then output : "Today"
Case 5. WEEK_IN_MILLIS then output : "12 June"

2. Use Library - Android Ago

We can use this library for relative time from now.
It contain custom TextView class and auto refresh Relative time.

Just add custom TextView on your xml layout and like below.


<com.github.curioustechizen.ago.RelativeTimeTextView
    android:id="@+id/timestamp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/margin_primary" />


Add below code in your activity


RelativeTimeTextView tvTimestamp = (RelativeTimeTextView) convertView.findViewById(R.id.timestamp);
tvTimestamp.setReferenceTime(<here pass timestamp>);


that's it.
More information about Library See Here
Thank you :)