I need to print date like today,Yesterday,2 days ago like that for that i have done
I am getting date like : String date1 = \"Thu Nov 13 19:01:25 GMT+05:30 2014\";
You can try this code: (delta is time in milliseconds)
public static String getDisplayableTime(long delta)
{
long difference=0;
Long mDate = java.lang.System.currentTimeMillis();
if(mDate > delta)
{
difference= mDate - delta;
final long seconds = difference/1000;
final long minutes = seconds/60;
final long hours = minutes/60;
final long days = hours/24;
final long months = days/31;
final long years = days/365;
if (seconds < 0)
{
return "not yet";
}
else if (seconds < 60)
{
return seconds == 1 ? "one second ago" : seconds + " seconds ago";
}
else if (seconds < 120)
{
return "a minute ago";
}
else if (seconds < 2700) // 45 * 60
{
return minutes + " minutes ago";
}
else if (seconds < 5400) // 90 * 60
{
return "an hour ago";
}
else if (seconds < 86400) // 24 * 60 * 60
{
return hours + " hours ago";
}
else if (seconds < 172800) // 48 * 60 * 60
{
return "yesterday";
}
else if (seconds < 2592000) // 30 * 24 * 60 * 60
{
return days + " days ago";
}
else if (seconds < 31104000) // 12 * 30 * 24 * 60 * 60
{
return months <= 1 ? "one month ago" : days + " months ago";
}
else
{
return years <= 1 ? "one year ago" : years + " years ago";
}
}
return null;
}
I am using the following the function to get RemainingTime but the format is almost same as you want. try this code and do changes as per your requirement.
/**
* To convert expire time according to selected format(period)
* @param period
* @return
*/
public String getExpireDate(int period) {
int days = 0, weeks = 0, hours = 0, mins = 0, secs = 0;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date systemDate = Calendar.getInstance().getTime();
String myDate = sdf.format(systemDate);
long millse = // your time in milliseconds
if (millse < 0) {
return activity.getResources().getString(R.string.already_expire);
}
long mills = Math.abs(millse);
if (mills > 60480000) {
weeks = (int) (mills / 604800000);
mills = mills % 604800000;
days = (int) (mills / 86400000);
mills = mills % 86400000;
hours = (int) (mills / 3600000);
mills = mills % 3600000;
mins = (int) (mills / 60000);
mills = mills % 60000;
secs = (int) (mills / 1000);
} else if (mills > 86400000) {
days = (int) (mills / 86400000);
mills = mills % 86400000;
hours = (int) (mills / 3600000);
mills = mills % 3600000;
mins = (int) (mills / 60000);
mills = mills % 60000;
secs = (int) (mills / 1000);
} else if (mills > 3600000) {
hours = (int) (mills / 3600000);
mills = mills % 3600000;
mins = (int) (mills / 60000);
mills = mills % 60000;
secs = (int) (mills / 1000);
}
String remainingTime = "";
switch (period) {
case SHORT_PERIOD:
if (weeks > 0) {
if (weeks == 1) {
remainingTime = weeks + " week";
} else {
remainingTime = weeks + " weeks";
}
} else if (days > 0) {
if (days == 1) {
remainingTime = days + " day";
} else {
remainingTime = days + " days";
}
} else if (hours > 0) {
if (hours == 1) {
remainingTime = hours + " hour";
} else {
remainingTime = hours + " hours";
}
} else if (mins > 0) {
if (mins == 1) {
remainingTime = mins + " minute";
} else {
remainingTime = mins + " minutes";
}
} else {
if (secs == 1) {
remainingTime = secs + " second";
} else {
remainingTime = secs + " seconds";
}
}
break;
case DETAILED_PERIOD:
if (weeks > 0) {
remainingTime = (weeks * 7) + days + " days," + hours
+ " hours," + mins + " minutes";// + secs + " secs";
} else if (days > 0) {
remainingTime = days + " days," + hours + " hours," + mins
+ " minutes";// + secs + " seconds";
} else if (hours > 0) {
remainingTime = hours + " hours," + mins + " minutes";// + secs
// +
// " seconds";
} else if (mins > 0) {
remainingTime = mins + " minutes";// + secs + " seconds";
}
break;
case EXACT_DATE_TIME:
SimpleDateFormat target = new SimpleDateFormat("dd-MMM-yyyy");
try {
remainingTime = target.format(sdf.parse(taskData
.getTask_expire_date()));
} catch (ParseException e) {
e.printStackTrace();
}
break;
default:
break;
}
return remainingTime;
}