从时间戳转换为日期 Android

4

你好,我有一个时间戳1481709600,我想将其转换为2016年12月14日 星期三这种格式。

我尝试使用以下方法:

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日。

7个回答

7
问题在于你的时间戳是以秒为单位,所以将时间戳转换成毫秒,然后使用日期格式函数...
尝试这个...
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);
    }

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

注:此处EEE表示星期几,MMM表示月份的英文缩写等。


2

如果你阅读这个,你可以制作任何组合。
你需要使用EEE, d MMM yyyy

更新

SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy");
sdf.format(new Date(1481709600));

0

日期格式错误;你必须使用

 new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z").format(date);

使用long代替integer - gauss

0
这对我有效:
val currentDay = Date()
    val day = currentDay.time
    Logger.i("currentDay = $currentDay and day : $day ")

    val c = Calendar.getInstance()
    c.timeInMillis = day
    val d = c.time
    val sdf = SimpleDateFormat("dd/MM/yyyy HH:mm")

    Logger.i("meDate : ${sdf.format(d)}")

0

尝试使用此方法获取数据。将时间作为long而不是int放入setTimeInMillis中。

private String getDate(long time) {
    Calendar cal = Calendar.getInstance(Locale.ENGLISH);
    cal.setTimeInMillis(time);
    String date = DateFormat.format("EEE MMM dd hh:mm:ss yyyy ", cal).toString();
    return date;
}

您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - Deepak Sachdeva

0

试一下这个

private String getDateFromTimeStamp(long time) {
        Calendar cal = Calendar.getInstance(Locale.ENGLISH);
        cal.setTimeInMillis(time);
        String date = DateFormat.format("EEE MMM dd hh:mm:ss yyyy ", cal).toString();
        return date;
    }

@VLeonovs,您传递的时间戳是多少?您犯的错误是将 Integer 值用作参数,实际上应该使用 long 数据类型,这就是为什么您会得到 1970 年的年份。请尝试使用上面的代码,并在获得相同输出时回复我。 - SaravInfern

0
您可以创建一个简单的方法来从时间戳中获取日期,如下所示。
public String getDateCurrentTimeZone(long timestamp) {
        try {
            Calendar calendar = Calendar.getInstance();
            TimeZone tz = TimeZone.getDefault();    calendar.setTimeInMillis(timestamp * 1000);
calendar.add(Calendar.MILLISECOND,
tz.getOffset(calendar.getTimeInMillis()));
            SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy");
Date currenTimeZone = (Date) calendar.getTime();
return sdf.format(currenTimeZone);
} catch (Exception e) {
}
return "";
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接