Created
March 17, 2016 07:22
-
-
Save VAdaihiep/0ee7c8f8cae65bad57ab to your computer and use it in GitHub Desktop.
Calculate and return time ago
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<resources> | |
<string name="time_ago_prefix"></string> | |
<string name="time_ago_suffix">trước</string> | |
<string name="time_ago_seconds">Vừa xong</string> | |
<string name="time_ago_minute">Khoảng 1 phút</string> | |
<string name="time_ago_minutes">%d phút</string> | |
<string name="time_ago_hour">Khoảng 1 giờ</string> | |
<string name="time_ago_hours">Khoảng %d giờ</string> | |
<string name="time_ago_day">một ngày</string> | |
<string name="time_ago_days">%d ngày</string> | |
<string name="time_ago_month">Khoảng 1 tháng</string> | |
<string name="time_ago_months">%d tháng</string> | |
<string name="time_ago_year">Khoảng 1 năm</string> | |
<string name="time_ago_years">%d năm</string> | |
</resources> | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Date; | |
import android.content.res.Resources; | |
public class TimeAgo { | |
public static String timeAgo(Date date) { | |
return timeAgo(date.getTime()); | |
} | |
public static String timeAgo(long millis) { | |
long diff = new Date().getTime() - millis; | |
Resources r = globalContext.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) { | |
return r.getString(R.string.time_ago_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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment