Volnetiks
Volnetiks

Reputation: 59

MMS and SMS dates aren't the same on Android

I have an android activity who loads every message for a thread id, and then show it, sort by date, i haven't any problems with it, before i tried to add mms, it looks like mms dates are not the same as sms date, i'm actually using android 7 on my phone for tests, and i would like it to works since android 5

I have already tried this: date = date * 1000; My date is a long get by cursor.getLong(2);

Here's all the query with mms

String selectionPart = "mid=" + mms.getString(mms.getColumnIndex("_id"));
                Uri uri = Uri.parse("content://mms/part");
                Cursor cursor = getContentResolver().query(uri, null,
                        selectionPart, null, null);
                if(cursor.moveToFirst()) {
                    do {
                        String type = cursor.getString(cursor.getColumnIndex("ct"));
                        if ("text/plain".equals(type)) {
                            String body;
                            String data = cursor.getString(cursor.getColumnIndex("_data"));
                            if (data != null) {
                                body = getMmsText(cursor.getString(cursor.getColumnIndex("_id")));
                            } else {
                                body = cursor.getString(cursor.getColumnIndex("text"));
                            }
                            long date = mms.getLong(2) * 1000L;
                            messages.add(new Message(body, new Date(date), number, true, image, name));
                            Log.d("datesss", String.valueOf(date));
                        }
                    } while(cursor.moveToNext());
                }

I would like to date looks the same, but actually, my sms dates looks like 1568780915460 and mms when like 1571992156 or 1571992156000 if i use * 1000

Upvotes: 1

Views: 181

Answers (1)

Joakim Danielson
Joakim Danielson

Reputation: 52053

Use Instant with different conversion methods

Instant instant1 = Instant.ofEpochSecond(1571992156);
System.out.println(instant1);
Instant instant2 = Instant.ofEpochMilli(1571992156000L);
System.out.println(instant2);

This will print

2019-10-25T08:29:16Z
2019-10-25T08:29:16Z

If you want to convert instant to a date type you can for instance do

LocalDateTime dateTime1 = LocalDateTime.ofInstant(instant1, ZoneOffset.UTC);
Date date1 = Date.from(instant1);

Upvotes: 1

Related Questions