Saturday, September 20, 2014

Timeago class in android

In this article we create Timeago function for change time.

For example AWeek ago, A Day ago, A Minute ago etc.

For Change your time in above format use below class for change your time to Timeago format.

Create object of TimeAgo class.
TimeAgo timeAgo = new TimeAgo(YOUR CLASS NAME HERE.this);
timeAgo.timeAgo(PASS YOUR STRING DATE HERE, PASS DATE FORMATTER HERE) ;
Below I describe the example with use of TimeAgo class.
public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TimeAgo timeago = new TimeAgo(MainActivity.this);
        String time1 = timeago.timeAgo("2014-09-20 00:10", "yyyy-MM-dd HH:mm");
        String time2 = timeago.timeAgo("2014-09-20 11:59", "yyyy-MM-dd HH:mm");
        String time3= timeago.timeAgo("2014-09-28 11:59", "yyyy-MM-dd HH:mm");
        TextView textView1 = (TextView) findViewById(R.id.textView1);
        TextView textView2 = (TextView) findViewById(R.id.textView2);
        TextView textView3 = (TextView) findViewById(R.id.textView3);
        textView1.setText(time1);
        textView2.setText(time2);
        textView3.setText(time3);
    }
}

Output of above example is like below.

First of add below this string to your string.xml file in your android project.

    <string name="time_ago_prefix"></string>

    <string name="time_ago_suffix">ago</string>

    <string name="time_ago_seconds">less than a minute</string>

    <string name="time_ago_minute">about a minute</string>

    <string name="time_ago_minutes">%d minutes</string>

    <string name="time_ago_hour">about an hour</string>

    <string name="time_ago_hours">about %d hours</string>

    <string name="time_ago_day">a day</string>

    <string name="time_ago_days">%d days</string>

    <string name="time_ago_month">about a month</string>

    <string name="time_ago_months">%d months</string>

    <string name="time_ago_year">about a year</string>

    <string name="time_ago_years">%d years</string>

Add this below TimeAgo.java  class in your package of android Project.
public class TimeAgo {
protected Context context;
public TimeAgo(Context context) {
this.context = context;
}

public String timeAgo(String strDate,String format) {
SimpleDateFormat formatter = new SimpleDateFormat(format);
try {
return timeAgo(formatter.parse(strDate));
} catch (ParseException e) {
e.printStackTrace();
}
return "";
}
public String timeAgo(Date date) {
return timeAgo(date.getTime());
}
public String timeAgo(long millis) {
long diff = new Date().getTime() - millis;
Resources r = context.getResources();
String prefix = r.getString(R.string.time_ago_prefix);
String suffix = r.getString(R.string.time_ago_suffix);
double seconds = Math.abs(diff) / 1000;
double minutes = seconds / 60;
double hours = minutes / 60;
double days = hours / 24;
double years = days / 365;
String words;
if (seconds < 45) {
words = r.getString(R.string.time_ago_seconds, Math.round(seconds));
} else if (seconds < 90) {
words = r.getString(R.string.time_ago_minute, 1);
} else if (minutes < 45) {
words = r.getString(R.string.time_ago_minutes, Math.round(minutes));
} else if (minutes < 90) {
words = r.getString(R.string.time_ago_hour, 1);
} else if (hours < 24) {
words = r.getString(R.string.time_ago_hours, Math.round(hours));
} else if (hours < 42) {
words = r.getString(R.string.time_ago_day, 1);
} else if (days < 30) {
words = r.getString(R.string.time_ago_days, Math.round(days));
} else if (days < 45) {
words = r.getString(R.string.time_ago_month, 1);
} else if (days < 365) {
words = r.getString(R.string.time_ago_months, Math.round(days / 30));
} else if (years < 1.5) {
words = r.getString(R.string.time_ago_year, 1);
} else {
words = r.getString(R.string.time_ago_years, Math.round(years));
}
StringBuilder sb = new StringBuilder();
if (prefix != null && prefix.length() > 0) {
sb.append(prefix).append(" ");
}
sb.append(words);
if (suffix != null && suffix.length() > 0) {
sb.append(" ").append(suffix);
}
return sb.toString().trim();
}
}
Hope this post is helpful for you.