[英]How to convert Timestamp to Date and calculate to "time ago"?
I have a Timestamp on mysql server.我在 mysql 服务器上有一个时间戳。 Example: "2019-11-23 14:26:11".
示例:“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
请帮忙
I think this answered question solves your's also.我认为这个回答的问题也解决了你的问题。
Android difference between Two Dates 两个日期之间的Android差异
Cast your String from the server to a Date object, then substract current day - what you got from the server.将您的 String 从服务器投射到 Date 对象,然后减去当天 - 您从服务器获得的内容。
Get current date with :获取当前日期:
import java.util.Calendar
Date currentTime = Calendar.getInstance().getTime();
use this method to return the time ago text setTime(Integer.valueOf(time_stamp));
使用此方法返回时间前文本
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.