Nitesh :
How to convert a String
date in time ago, date format like yyyy-dd-mm hh:ss:mm
, time ago that means day , month , years , hours
private String CalculateDatefromcurrentdate(String ago_date, String currant_date) {
String setalldate = "";
try {
SimpleDateFormat convert_date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
Calendar calendar1 = Calendar.getInstance();
calendar1.setTime(convert_date.parse(ago_date));
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(convert_date.parse(currant_date));
long millis1 = calendar1.getTimeInMillis();
long millis2 = calendar2.getTimeInMillis();
long diff = millis2 - millis1;
// Calculate difference in seconds
long diffSeconds = diff / 1000;
// Calculate difference in minutes
long diffMinutes = diff / (60 * 1000);
// Calculate difference in hours
long diffHours = diff / (60 * 60 * 1000);
// Calculate difference in days
long diffDays = diff / (24 * 60 * 60 * 1000);
// calculate how much month in days
long multipledays = diffDays;
int maonths = (int) multipledays / 30;
int reamainigdays = maonths % 30;
// Calculate the years of monthes
int getyears = maonths / 12;
int reamainigmothe = getyears % 12;
if (diffSeconds < diff / 1000) {
setalldate = diffSeconds + "Second ago";
} else if (diffMinutes < diff / (60 * 1000)) {
setalldate = diffMinutes + "minutes ago";
} else if (diffHours < diff / (60 * 60 * 1000) ) {
setalldate = diffHours + "hr ago";
} else if (diffDays < diff / (24 * 60 * 60 * 1000)) {
setalldate = diffDays + "day ago";
} else if (reamainigdays < maonths % 30) {
setalldate = reamainigdays + "months ago";
} else if (reamainigmothe < getyears % 12) {
setalldate = reamainigmothe + "years ago";
}
System.out.println("In milliseconds: " + diff + " milliseconds.");
System.out.println("In seconds: " + diffSeconds + " seconds.");
System.out.println("In minutes: " + diffMinutes + " minutes.");
System.out.println("In hours: " + diffHours + " hours.");
System.out.println("In days: " + diffDays + " days.");
System.out.println("In days: " + reamainigdays + " months.");
System.out.println("In days: " + reamainigdays + " reamaingdays.");
System.out.println("In days: " + getyears + " years.");
System.out.println("In days: " + reamainigmothe + " month in this years.");
// Toast.makeText(this, "days=" + daysDiff, Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
return setalldate;
}
Rakesh Kumar :
You can do this following with your existing the following:
String convTime = null;
try {
String ago_date = "2020-01-14 12:52:00", currant_date = "2020-01-18 12:52:45";
String suffix = "Ago";
SimpleDateFormat convert_date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
Calendar calendar1 = Calendar.getInstance();
calendar1.setTime(convert_date.parse(ago_date));
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(convert_date.parse(currant_date));
long millis1 = calendar1.getTimeInMillis();
long millis2 = calendar2.getTimeInMillis();
long diff = millis2 - millis1;
// Calculate difference in seconds
long diffSeconds = diff / 1000;
// Calculate difference in minutes
long diffMinutes = diff / (60 * 1000);
// Calculate difference in hours
long diffHours = diff / (60 * 60 * 1000);
// Calculate difference in days
long diffDays = diff / (24 * 60 * 60 * 1000);
// calculate how much month in days
long multipledays = diffDays;
int maonths = (int) multipledays / 30;
int reamainigdays = maonths % 30;
// Calculate the years of monthes
int getyears = maonths / 12;
int reamainigmothe = getyears % 12;
System.out.println("In calculated: ");
if (diffSeconds<60) {
convTime = diffSeconds+" Seconds "+suffix;
} else if (diffMinutes < 60) {
convTime = diffMinutes+" Minutes "+suffix;
} else if (diffHours < 24) {
convTime = diffHours+" Hours "+suffix;
} else if (diffDays >= 7) {
if (reamainigmothe!=0 && reamainigmothe <= getyears % 12) {
convTime = reamainigmothe + " Years " + suffix;
} else if (diffDays > 30) {
convTime = maonths + " Months " + suffix;
} else {
convTime = (diffDays / 7) + " Week " + suffix;
}
} else if (diffDays < 7) {
convTime = diffDays+" Days "+suffix;
}
System.out.println("In calculated: 222--"+convTime );
} catch (Exception e) {
e.printStackTrace();
}
Where ago_date
will be the past date and currant_date
needs to be replaced with the current date, which will work you.
String ago_date = "2020-01-14 12:52:00", currant_date = "2020-01-18 12:52:45";
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=358100&siteId=1