Last active
September 29, 2016 13:18
-
-
Save yagneshshinde/f29b714c279d464f7fc41aca562938ae to your computer and use it in GitHub Desktop.
Time difference (Just now,a min ago, a day ago, etc ...)
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
<?xml version="1.0" encoding="utf-8"?> | |
<resources> | |
<string name="time_seconds">Just Now</string> | |
<plurals name="time_minute"> | |
<item quantity="one">%d minute</item> | |
<item quantity="other">%d minutes</item> | |
</plurals> | |
<plurals name="time_day"> | |
<item quantity="one">%d day</item> | |
<item quantity="other">%d days</item> | |
</plurals> | |
<plurals name="time_hour"> | |
<item quantity="one">%d hour</item> | |
<item quantity="other">%d hours</item> | |
</plurals> | |
<plurals name="time_year"> | |
<item quantity="one">%d year</item> | |
<item quantity="other">%d years</item> | |
</plurals> | |
<plurals name="time_month"> | |
<item quantity="one">%d month</item> | |
<item quantity="other">%d months</item> | |
</plurals> | |
<string name="time_ago">ago</string> | |
<string name="time_from_now">from now</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
//In you main activity just initilize TimeAgo class | |
TimeAgo timeago=new TimeAgo(context.getResources()); | |
timeago.timeAgo("Time In milisecond of Date") |
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 android.content.res.Resources; | |
import java.util.Date; | |
public class TimeAgo { | |
private final Resources resources; | |
public TimeAgo(Resources resources) { | |
this.resources = resources; | |
} | |
/** | |
* Get time ago that date occurred | |
* | |
* @param date | |
* @return time string | |
*/ | |
public String timeAgo(final Date date) { | |
return timeAgo(date.getTime()); | |
} | |
/** | |
* Get time ago that milliseconds date occurred | |
* | |
* @param millis | |
* @return time string | |
*/ | |
public String timeAgo(final long millis) { | |
return time(System.currentTimeMillis() - millis); | |
} | |
/** | |
* Get time string for milliseconds distance | |
* | |
* @param distanceMillis | |
* @return time string | |
*/ | |
public String time(Long distanceMillis) { | |
final double seconds = distanceMillis / 1000; | |
final double minutes = seconds / 60; | |
final double hours = minutes / 60; | |
final double days = hours / 24; | |
final double years = days / 365; | |
final String time; | |
if (seconds < 45) { | |
time = resources.getString(R.string.time_seconds); | |
} else if (seconds < 90 || minutes < 45) { | |
time = resources.getQuantityString(R.plurals.time_minute, minutes < 2 ? 1 : 2, Math.round(minutes)); | |
} else if (minutes < 90 || hours < 24) { | |
time = resources.getQuantityString(R.plurals.time_hour, hours < 2 ? 1 : 2, Math.round(hours)); | |
} else if (hours < 48 || days < 30) { | |
time = resources.getQuantityString(R.plurals.time_day, days < 2 ? 1 : 2, Math.round(days)); | |
} else if (days < 60 || days < 365) { | |
time = resources.getQuantityString(R.plurals.time_month, (days / 30) < 2 ? 1 : 2, Math.round(days / 30)); | |
} else { | |
time = resources.getQuantityString(R.plurals.time_year, years < 2 ? 1 : 2, Math.round(years)); | |
} | |
if (time.equals(resources.getString(R.string.time_seconds))) { | |
return time; | |
} else { | |
return time + " " + resources.getString(R.string.time_ago); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment