How do I get difference between two dates in android?, tried every thing and post

后端 未结 11 2262
天命终不由人
天命终不由人 2020-11-28 09:06

I saw all the post in here and still I can\'t figure how do get difference between two android dates.

This is what I do:

long diff = date1.getTime()          


        
11条回答
  •  自闭症患者
    2020-11-28 09:40

    If you use Kotlin language for Android development, you can use ExperimentalTime extension. To get difference in days you can use it like this:

    @ExperimentalTime
    fun daysDiff(c1: Calendar, c2: Calendar): Double {
        val diffInMillis = c1.timeInMillis - c2.timeInMillis
        return diffInMillis.milliseconds.inDays
    }
    

    or if you want to get results as integer:

    @ExperimentalTime
    fun daysDiff2(c1: Calendar, c2: Calendar): Int {
        val diffInMillis = c1.timeInMillis - c2.timeInMillis
        return diffInMillis.milliseconds.toInt(DurationUnit.DAYS)
    }
    

提交回复
热议问题