To Get Result
// time is String and result is String.
TimeAgo2 timeAgo2 = new TimeAgo2();
result = timeAgo2.covertTimeToText(time);
JAVA code
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import java.text.ParseException;
import android.util.Log;
// Created by D.satish (imsatish709) instagram //
public class TimeAgo2 {
public String covertTimeToText(String dataDate) {
String convTime = null;
String prefix = "";
String suffix = "Ago";
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
Date pasTime = dateFormat.parse(dataDate);
Date nowTime = new Date();
long dateDiff = nowTime.getTime() - pasTime.getTime();
long second = TimeUnit.MILLISECONDS.toSeconds(dateDiff);
long minute = TimeUnit.MILLISECONDS.toMinutes(dateDiff);
long hour = TimeUnit.MILLISECONDS.toHours(dateDiff);
long day = TimeUnit.MILLISECONDS.toDays(dateDiff);
if (second < 60) {
convTime = second + " Seconds " + suffix;
} else if (minute < 60) {
convTime = minute + " Minutes "+suffix;
} else if (hour < 24) {
convTime = hour + " Hours "+suffix;
} else if (day >= 7) {
if (day > 360) {
convTime = (day / 360) + " Years " + suffix;
} else if (day > 30) {
convTime = (day / 30) + " Months " + suffix;
} else {
convTime = (day / 7) + " Week " + suffix;
}
} else if (day < 7) {
convTime = day+" Days "+suffix;
}
} catch (ParseException e) {
e.printStackTrace();
Log.e("ConvTimeE", e.getMessage());
}
return convTime;
}
}
0 Comments