Home > other >  If condition between two date by hour
If condition between two date by hour

Time:05-19

I'm makeing an app that want some text color Change when current time between 2 date.

like I have schedule with tasks, and from 1:00PM to 5:00PM there is a task I have to do.

Want to make a condition if current time between two date change the color of this text.

it's not just about color.

it's a lot of things put it's depend on this condition

Puted the two date in one TextView

        val simpleDateFormat = SimpleDateFormat("h:mm a")
    val time = simpleDateFormat.format(item.startDate)   "-"   simpleDateFormat.format(item.endDate)

CodePudding user response:

looks like your item.startDate and item.endDate are Date instances. so you need also a Date with current time, which you can get with

val currDate = Calendar.getInstance().getTime()

or even by creating new Date instance (is set to to current by default)

val currDate = Date()

now you can convert Date to long timestamp

val startDateAsTimestamp = item.startDate.getTime()
val endDateAsTimestamp = item.endDate.getTime()
val currDateAsTimestamp = currDate.getTime()

and now your if would be

if (currDateAsTimestamp >= startDateAsTimestamp &&
     currDateAsTimestamp <= endDateAsTimestamp) {

CodePudding user response:

couldn't put it in the comment

the code :

 val currDate = Date()
    val startDateAsTimestamp = item.startDate.getTime()
    val currDateAsTimestamp = currDate.getTime()
    val endDateAsTimestamp = item.endDate.getTime()
    println("FFFFF time startDateAsTimestamp is : $startDateAsTimestamp")
    println("SSSSS time currDateAsTimestamp is : $currDateAsTimestamp")
    println("BBBBB time endDateAsTimestamp is : $endDateAsTimestamp")

the output :

I/System.out: FFFFF time startDateAsTimestamp is : 1561441500000

I/System.out: SSSSS time currDateAsTimestamp is : 1652944971953

I/System.out: BBBBB time endDateAsTimestamp is : 1561448700000

currDateAsTimestamp always bigger

  • Related