开发者

Gettin date from a SMS message android [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Android - get date time from SMS timestamp in miliseconds

I am using the following code to record sms messages:

    public void getColumnData(Cursor cur, Context context) {
    ArrayList<String> exportBuffer = new ArrayList<String>();
    try {
        if (cur.moveToFirst()) {
            String id;
            String date;
            String phoneNumber;
            String body;

            int idColumn = cur.getColumnIndex("_id");
            int dateColumn = cur.getColumnIndex("date");
            int numberColumn = cur.getColumnIndex("address");
            int bodyColumn = cur.getColumnIndex("body");

            do {
                id = cur.getString(idColumn);
                date = cur.getString(dateColumn);
                body = cur.getString(bodyColumn);
                phoneNumber = cur.getString(numberColumn);

                String FormattedDate;

                SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss dd-MM-yyyy");
                FormattedDate = sdf.format(date).toString();

                exportBuffer.add(id + " ," + FormattedDate + " ," + body + " ,"
                        + phoneNumber);
            } while (cur.moveToNext());
        }
        WriteToFile(exportBuffer,context);
    } catch (Exception e) {
        int MessageDuration = Toast.LENGTH_SHORT;       
        CharSequence text = "An Error Occurred, Code:104";  
        Toast toast = Toast.makeText(context, text, MessageDuration);
        toast.show();
    }
}

It all works fine apart from the date string I have no idea what format it is in which means I can't format it to a correct date here is an example o开发者_如何学运维f one of the dates:

1309817682651

Thanks in advance.


Try using this piece of code:

public static String millisToDate(long currentTime) {
    String finalDate;
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(currentTime);
    Date date = calendar.getTime();
    finalDate = date.toString();
    return finalDate;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜