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
264 views
in Technique[技术] by (71.8m points)

java - How to display date on item in ListView?

Hi guys i want to diplay date for every item in listview. I tried something but it shows a number not in date format. I think that i need to convert that number but i don't know how. This is my code. Thanks to evryone in advance

public void getAudio() {
        ContentResolver contentResolver = getContentResolver();
        Uri audioUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        Cursor audioCursor = contentResolver.query(audioUri, null, null, null, null);

        if(audioCursor != null && audioCursor.moveToFirst()) {
            int audioTitle = audioCursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
            int audioDate = audioCursor.getColumnIndex(MediaStore.Audio.Media.DATE_ADDED);

            do {
                String currentTitle = audioCursor.getString(audioTitle);
                String currentDate = audioCursor.getString(audioDate);
                arrayList.add(currentTitle + "
" + currentDate);
            }
            while (audioCursor.moveToNext());
        }
    }

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

1 Reply

0 votes
by (71.8m points)

There are 2 things to consider here. First, you have to convert that number/string into date object in java. Secondly, once you have date object, you can convert it to any date format you would love. For example Dec 23, 2020 etc.

If you have date in milliseconds you can use like this

1- Date currentDate = new Date(currentDateTime); //creating Date from millisecond

2- Now we have date object, it can be formatted now.

DateFormat df = new SimpleDateFormat("dd:MM:yy:HH:mm:ss");
           //formatted value of current Date
           System.out.println("Milliseconds to Date: " + df.format(currentDate));

Let me know, if it is helpful for you.


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

...