mardi 31 août 2021

How to compare current date with dates from API to display different ImageView if the date from API greater, less, or equal to current date in Kotlin

I am getting dates from API based on what the user posted to the API

what i am trying to do here is displaying different ImageView if the date from API is greater, less, or equal to the current date

i have this bind function:

fun bind(event: PlannerGet) {
        val stringzz = event.date
        val date = LocalDate.parse(stringzz, DateTimeFormatter.ISO_DATE)
        val dateFormatter: DateTimeFormatter = DateTimeFormatter.ofPattern("dd-MMM-uuuu")
        val sdf = SimpleDateFormat("dd-MMM-yyyy")
        val c = Calendar.getInstance()
        try {
            c.time = sdf.parse(currentDate("dd-MMM-yyyy"))
        } catch (e: ParseException) {
            e.printStackTrace()
        }
        val _date: String = sdf.format(c.time)
        val gregorianDate: LocalDate = LocalDate.parse(_date, dateFormatter)
        val islamicDate: HijrahDate = HijrahDate.from(date)


        mTextHijri.text = islamicDate.format(dateFormatter).toString()
        mTextNote.text = "${event.note}"
        mTextTitle.text = "${event.title} at ${event.location}"
        mTextIndex.text = "${getTime(event.startTime!!)} - ${getTime(event.endTime!!)}"
        mTextDate.text = (date.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL)))
        mTextTimeEstimated.text = getEstimated(getTime(event.startTime), getTime(event.endTime))

        
    }

and it is working fine, i tried to add if condition to the function like this:

if(mTextDate.toString() < _date){
            mTick.setImageResource(R.drawable.tick)
        }
        else if (mTextDate.toString() > _date){
            mTick.setImageResource(R.drawable.ic_logopng)
        }
        else if (mTextDate.toString() == _date){
            mTick.setImageResource(R.drawable.blue_logo)
        }

and i already posted 3 different dates in the API to compare but i ended up with the same ImageView every time, what am i doing wrong here?

and i also tried to compare straight away from the data class (Data class below):

data class PlannerGet(
val date: String,
val endTime: String,
val id: Int,
val location: String,
val note: String,
val startTime: String,
val title: String
)

but i ended up with the same ImageView every time

any suggestion to make this work?

Aucun commentaire:

Enregistrer un commentaire