-2

I want to know if there any getter options to get time,date etc associated with Date() built-in class in kotlin..? If yes then which are the options and how to use them..?

I have searched a lot but found nothing..I have provided my code below..

Inbox Activity

 private fun refreshSmsInbox() {

            try {


                val smsList = ArrayList<SmsData>()
                val cursor = contentResolver.query(Uri.parse("content://sms/inbox"),null,null,null,null)
                cursor?.let{
                    if(it!!.moveToFirst()){
                        val nameID = it.getColumnIndex("address")
                        val messageID = it.getColumnIndex("body")
                        //val dateID = it.getColumnIndex("date")
                       val timestamp = it.getColumnIndexOrThrow("date")


                        do{
                            val dateString = it.getString(timestamp)
                            val date : Date = Date(dateString.toLong())
                            val formatter = SimpleDateFormat("hh:mm a")
                            val displayTime = formatter .format(date)


                          val sms = SmsData(getContactName(this,it.getString(nameID!!.toInt()).toString()),it.getString(messageID),displayTime)

                            smsList.add(sms)

                        }while (it.moveToNext())
                    }
                    it.close()
                }
                val  adapter = ListAdapter(this, smsList)
                sms_list_view.adapter = adapter


    } catch (ex: Exception) {
                if (this != null) {
                    Toast.makeText(this, ex.localizedMessage, Toast.LENGTH_SHORT).show()
                }
            }

    }

List Adapter

class ListAdapter (val context: Context, val list : ArrayList<SmsData>): BaseAdapter(){
    @SuppressLint("ViewHolder", "SimpleDateFormat")
    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {

        val view = LayoutInflater.from(context).inflate(R.layout.rowlayout,parent,false)

        /*view.clickable.setOnClickListener {
            val intent = Intent(this, MainActivity1::class.java)

            startActivity(intent)
        }*/
        list[position].senderName?.let{
            view.sender.text = it.substring(0,1).toUpperCase()
        }

        view.sms_sender.text = list[position].senderName

        view.sms_message.text = list[position].message

        view.sms_date.text = list[position].date.toString()

        view.clickable.setOnClickListener { View ->

            val intent = Intent(context,MainActivity1::class.java)
            intent.putExtra("address",list[position].senderName)
                    context.startActivity(intent)
        }

        return  view


    }

Model class

data class SmsData(val senderName: String?, val message: String, val date: Date){ }

Expected:

Time 11:45 am/pm Actual:

Mon 12 jul 2019 GMT +05:00

2 Answers2

0

to answer your question, you need something like this :

    val date = Date()
    val formatter = SimpleDateFormat("hh:mm a")
    val displayTime = formatter .format(date)

this gives you : 09:00 PM

a_local_nobody
  • 7,947
  • 5
  • 29
  • 51
  • Wow you are great ...It resolved my problem..Thanks:) –  Aug 08 '19 at 11:29
  • But how to change to date when that day passes..? –  Aug 08 '19 at 11:45
  • You mean we have to save dates in a database and then check it from that ..? –  Aug 08 '19 at 12:01
  • thanks. all you have to do is, when you inflate each item in to your list, check that the date of that item is older than today, if it is older than today, show the full date, otherwise use my answer here – a_local_nobody Aug 08 '19 at 12:01
  • you mean I need to set this in the list adapter of the inbox..? –  Aug 08 '19 at 12:05
  • I have posted my full code now. I get your point but do not understand how to apply this in my code..Can you show me in the code..? –  Aug 08 '19 at 12:20
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/197684/discussion-between-aisha-and-a-local-nobody). –  Aug 08 '19 at 12:28
-1

Try this:

val dateString = it.getString(timestamp)
val dateFormat = SimpleDateFormat("HH:mm a")
dateFormat.timeZone = TimeZone.getTimeZone("GMT") //Your timezone if necessary
val date = dateFormat.format(dateString)

Hope it helps.

Oye
  • 73
  • 1
  • 8