для этого я сделал Just Now, seconds ago, min ago, hrs ago, days ago, weeks ago, months ago, years ago
, в этом примере вы можете разобрать дату как 2018-09-05T06:40:46.183Z
эту или любую другую, как показано ниже
добавить значение ниже string.xml
<string name="lbl_justnow">Just Now</string>
<string name="lbl_seconds_ago">seconds ago</string>
<string name="lbl_min_ago">min ago</string>
<string name="lbl_mins_ago">mins ago</string>
<string name="lbl_hr_ago">hr ago</string>
<string name="lbl_hrs_ago">hrs ago</string>
<string name="lbl_day_ago">day ago</string>
<string name="lbl_days_ago">days ago</string>
<string name="lbl_lstweek_ago">last week</string>
<string name="lbl_week_ago">weeks ago</string>
<string name="lbl_onemonth_ago">1 month ago</string>
<string name="lbl_month_ago">months ago</string>
<string name="lbl_oneyear_ago" >last year</string>
<string name="lbl_year_ago" >years ago</string>
java код попробуйте ниже
public String getFormatDate(String postTime1) {
Calendar cal=Calendar.getInstance();
Date now=cal.getTime();
String disTime="";
try {
Date postTime;
//2018-09-05T06:40:46.183Z
postTime = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").parse(postTime1);
long diff=(now.getTime()-postTime.getTime()+18000)/1000;
//for months
Calendar calObj = Calendar.getInstance();
calObj.setTime(postTime);
int m=calObj.get(Calendar.MONTH);
calObj.setTime(now);
SimpleDateFormat monthFormatter = new SimpleDateFormat("MM"); // output month
int mNow = Integer.parseInt(monthFormatter.format(postTime));
diff = diff-19800;
if(diff<15) { //below 15 sec
disTime = getResources().getString(R.string.lbl_justnow);
} else if(diff<60) {
//below 1 min
disTime= diff+" "+getResources().getString(R.string.lbl_seconds_ago);
} else if(diff<3600) {//below 1 hr
// convert min
long temp=diff/60;
if(temp==1) {
disTime= temp + " " +getResources().getString(R.string.lbl_min_ago);
} else {
disTime = temp + " " +getResources().getString(R.string.lbl_mins_ago);
}
} else if(diff<(24*3600)) {// below 1 day
// convert hr
long temp= diff/3600;
System.out.println("hey temp3:"+temp);
if(temp==1) {
disTime = temp + " " +getResources().getString(R.string.lbl_hr_ago);
} else {
disTime = temp + " " +getResources().getString(R.string.lbl_hrs_ago);
}
} else if(diff<(24*3600*7)) {// below week
// convert days
long temp=diff/(3600*24);
if (temp==1) {
// disTime = "\nyesterday";
disTime = temp + " " +getResources().getString(R.string.lbl_day_ago);
} else {
disTime = temp + " " +getResources().getString(R.string.lbl_days_ago);
}
} else if(diff<((24*3600*28))) {// below month
// convert week
long temp=diff/(3600*24*7);
if (temp <= 4) {
if (temp < 1) {
disTime = getResources().getString(R.string.lbl_lstweek_ago);
}else{
disTime = temp + " " + getResources().getString(R.string.lbl_week_ago);
}
} else {
int diffMonth = mNow - m;
Log.e("count : ", String.valueOf(diffMonth));
disTime = diffMonth + " " + getResources().getString(R.string.lbl_month_ago);
}
}else if(diff<((24*3600*365))) {// below year
// convert month
long temp=diff/(3600*24*30);
System.out.println("hey temp2:"+temp);
if (temp <= 12) {
if (temp == 1) {
disTime = getResources().getString(R.string.lbl_onemonth_ago);
}else{
disTime = temp + " " + getResources().getString(R.string.lbl_month_ago);
}
}
}else if(diff>((24*3600*365))) { // above year
// convert year
long temp=diff/(3600*24*30*12);
System.out.println("hey temp8:"+temp);
if (temp == 1) {
disTime = getResources().getString(R.string.lbl_oneyear_ago);
}else{
disTime = temp + " " + getResources().getString(R.string.lbl_year_ago);
}
}
} catch(Exception e) {
e.printStackTrace();
}
return disTime;
}