I have a Timestamp on mysql server. Example: "2019-11-23 14:26:11". I get a Timestamp in the form of a String from the server, and further on my idea, I should convert the Timestamp to Date and then count "time ago". Please, help
Asked
Active
Viewed 374 times
2 Answers
1
I think this answered question solves your's also.
Android difference between Two Dates
Cast your String from the server to a Date object, then substract current day - what you got from the server.
Get current date with :
import java.util.Calendar
Date currentTime = Calendar.getInstance().getTime();

miguelik
- 475
- 4
- 11
0
use this method to return the time ago text
setTime(Integer.valueOf(time_stamp));
private String setTime(int date) {
String time_text = "";
long time = System.currentTimeMillis() / 1000;
long mills = time - date;
int hours = safeLongToInt(mills / (60 * 60));
int mins = safeLongToInt((mills - (hours * 3600) / (60)) % 60);
String diff = hours + ":" + mins;
if (hours < 24) {
duration.setText(diff + " hours ago");
} else {
Date dateTemp = new Date(date * 1000L);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss ");
sdf.setTimeZone(getTimeZone("GMT+2"));
time_text = sdf.format(dateTemp);
return time_text;
}
return time_text;
}

Mohamed Mo'nes
- 420
- 3
- 14
-
1`setTime(Integer.valueOf(time_stamp));` What do you think `Integer.valueOf("2019-11-23 14:26:11")` will return? – blackapps Nov 24 '19 at 14:43
-