How to convert date format from Android messages

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 a date in this format "1352933381889"
How to convert to regular date / time format in String?

+4
source share
2 answers

Try the following:

 Date date = new Date(cursor1.getLong(0)); String formattedDate = new SimpleDateFormat("MM/dd/yyyy").format(date); 
+15
source

It seems that you are getting the date in milliseconds. To convert miliseconds to a Date object:

 long milliSeconds = cursor1.getString(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()); 
+6
source

Source: https://habr.com/ru/post/1446211/


All Articles