android时间戳解析出错(始终在1970年) [英] android timestamp parsing gone wrong(always in 1970)

查看:623
本文介绍了android时间戳解析出错(始终在1970年)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

im试图将字符串(带有unix时间戳)转换为格式为(dd-MM-yyyy)的日期

im trying to convert a string(with unix timestamp) to an date with the format ( dd-MM-yyyy)

,这部分起作用.我现在遇到的问题是我的约会日期是1970年1月17日(而不是2015年3月16日)

and this is working partly. The problem im having now is that my date is in 17-01-1970 (instead of march 16 2015)

im像这样转换它:

SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
    Date d = null;
    int dateMulti = Integer.parseInt(Date);
    Calendar cal = Calendar.getInstance(Locale.ENGLISH);
    cal.setTimeInMillis(dateMulti);
    String date = DateFormat.format("dd-MM-yyyy", cal).toString();

    Log.d("test",date);
    try {
        d = dateFormat.parse(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }

其中日期= 1427101853 结果= 17-01-1970

where Date = 1427101853 and the result = 17-01-1970

我在做什么错了?

推荐答案

第一行中使用了错误的格式字符串:

You are using the wrong format string in the first line:

SimpleDateFormat dateFormat = new SimpleDateFormat("dd-mm-yyyy");

mm是分钟.改为使用MM(月).

mm is minutes. Use MM (months) instead.

edit Unix时间戳自格林尼治标准时间1970年1月1日00:00:00起为. Java自格林尼治标准时间1970年1月1日0时以来以毫秒为单位进行测量.您需要将Unix时间戳乘以1000:

edit A Unix timestamp is a number of seconds since 01-01-1970 00:00:00 GMT. Java measures time in milliseconds since 01-01-1970 00:00:00 GMT. You need to multiply the Unix timestamp by 1000:

cal.setTimeInMillis(dateMulti * 1000L);

这篇关于android时间戳解析出错(始终在1970年)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆