你好,我有时间戳1481709600,我想要这个时间格式的Wed, 14 Dec 2016
我试着用:
private String getDateFromTimeStamp(Integer dt) {
Date date = new Date (dt);
return new SimpleDateFormat("EEE MMM dd hh:mm:ss yyyy ").format(date);
}但是当前的输出是Sun Jan 18 05:35:09 GMT+02:00 1970
我认为日期格式是错误的,我需要使用哪一种?
谢谢!
更新
问题是年月日错了,应该是2016年12月14日,而不是1970年1月18日。
发布于 2016-12-14 09:57:30
问题是timeStamp以秒为单位,所以将timeStamp转换为millisec,然后使用日期格式函数.
试试这个..。
JAVA
private String getDate(long time) {
Date date = new Date(time*1000L); // *1000 is to convert seconds to milliseconds
SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy "); // the format of your date
sdf.setTimeZone(TimeZone.getTimeZone("GMT-4"));
return sdf.format(date);;
}Kotlin
fun getDate(time:Long):String {
var date:Date = Date(time*1000L); // *1000 is to convert seconds to milliseconds
var sdf:SimpleDateFormat = SimpleDateFormat("EEE, dd MMM yyyy "); // the format of your date
sdf.setTimeZone(TimeZone.getTimeZone("GMT-4"));
return sdf.format(date);
}输出:-像这样

注:- EEE表示为一周中的一天,MMM表示为单词中的月份,等等。
发布于 2016-12-14 09:43:08
如果你读这,你可以做任何组合
您需要使用EEE, d MMM yyyy
更新
SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy");
sdf.format(new Date(1481709600));发布于 2016-12-14 09:42:58
日期格式是错误的;您必须使用
new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z").format(date);https://stackoverflow.com/questions/41139218
复制相似问题