1

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?

UPDATE:

i tried to use Log as "Akhil" mentioned in the comments,

so i wanted to compare two timestamps like this:

var currentDateTime=LocalDateTime.now()
        var time= currentDateTime.format(DateTimeFormatter.ofPattern("HH:mm"))
        Log.d("Time", time)
if(event.startTime > time){

            mTextTimeEstimated.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.tick_ongoing_very_small, 0);
        }
        else if (event.startTime < time){
            mTextTimeEstimated.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.tick_upcoming_very_small, 0);
        }
        else if (event.startTime < time) {
            mTextTimeEstimated.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.tick_completed_very_small, 0);
        }

and the log is as follow:

D/Time: 17:23
D/event time: 18:19
D/Time: 17:23
D/event time: 08:36
D/Time: 17:23
D/event time: 05:37
D/Time: 17:23
D/event time: 04:38

but still getting one value which is:

event.startTime > time
Enigma
  • 353
  • 4
  • 14
  • You could try parsing the strings to dates and comparing them instead of comparing the strings. The problem with strings is that "12-04-2020" > "11-05-2020" returns true but when you consider them as dates it should return false. – Akhil Aug 31 '21 at 06:55
  • Could you paste the values of `mTextDate.toString()` and `_date` when printed? – Akhil Aug 31 '21 at 07:04
  • i am getting this value always "ic_logopng" – Enigma Aug 31 '21 at 07:56
  • 1
    I meant could you log the values of `mTextDate.toString()` and `_date` and paste it here? – Akhil Aug 31 '21 at 09:52
  • Could you please comment if your problem was solved? It helps others with the same or similar problems. – Akhil Sep 01 '21 at 09:28
  • i'm still trying to solve the problem yet i didn't manage to solve it, i have updated my question with what i have tried to do – Enigma Sep 01 '21 at 12:17
  • Like I said, please do not compare date/time as strings. Import `LocalTime` and compare using `LocalTime.parse(event.startTime) < LocalTime.parse(time)`. Somebody has already linked this question to an appropriate answer as to why this is the best way to do it. – Akhil Sep 02 '21 at 17:29
  • 1
    i was able to compare it but the problem was i wasn't using the same format, now its working, thank you! – Enigma Sep 03 '21 at 05:00

0 Answers0