从时间戳到日期 Android

VLe*_*ovs 4 android timestamp date kotlin

美好的一天,我有时间戳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);
}
Run Code Online (Sandbox Code Playgroud)

但目前的输出是 Sun Jan 18 05:35:09 GMT+02:00 1970

我认为日期格式错误,我需要使用哪一种?

谢谢!

更新

问题是年份和月份是错误的,应该是 2016 年 12 月 14 日而不是 1970 年 1 月 18 日

sus*_*dlh 7

问题是您的 timeStamp 以秒为单位,因此将您的 timeStamp 转换为毫秒,然后使用日期格式函数...

试试这个...

爪哇

 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);;
    }
Run Code Online (Sandbox Code Playgroud)

科特林

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);
    }
Run Code Online (Sandbox Code Playgroud)

输出:-像这样 2016 年 12 月 14 日,星期三

注意:-EEE在周中表示为天,MMM在单词中表示为月等..