Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
624 views
in Technique[技术] by (71.8m points)

date - android timestamp parsing gone wrong(always in 1970)

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

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 converting it like this:

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();
    }

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

what am i doing wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

mm is minutes. Use MM (months) instead.

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...