How to convert date format from Android inbox sms
Asked Answered
J

2

8

I used this code:

String[] columnDate =   new String[] {"date"};
Cursor cursor1 = getContentResolver().query(Uri.parse("content://sms/inbox"),
    columnDate ,null, null, "date desc limit 1");
cursor1.moveToPosition(0);
String msgData1Date = cursor1.getString(0);

..and it works but gives date on this format "1352933381889"
How to convert to normal date/time format in String type?

Jinja answered 15/11, 2012 at 12:1 Comment(0)
P
16

Try this:

Date date = new Date(cursor1.getLong(0));
String formattedDate = new SimpleDateFormat("MM/dd/yyyy").format(date);
Pasquil answered 15/11, 2012 at 12:3 Comment(0)
W
9

It seems that you are receiving the date in miliseconds. To convert miliseconds into a Date object:

long milliSeconds = cursor1.getLong(0);
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss.SSS");
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliSeconds);
String finalDateString = formatter.format(calendar.getTime());
Ware answered 15/11, 2012 at 12:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.