1

I have to calculate how many days are left between the date selected by the user through a DatePicker and the current date

I was trying to write something like this:

val simpleDate = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
val date = simpleDate.parse(event!!.date!!)
val diff = Duration.between(LocalDate.now(), date.toInstant())
val leftDays = diff.toDays()
deHaar
  • 17,687
  • 10
  • 38
  • 51
  • Does this answer your question? [How do I get difference between two dates in android?, tried every thing and post](https://stackoverflow.com/questions/10690370/how-do-i-get-difference-between-two-dates-in-android-tried-every-thing-and-pos) – abdo Salm Sep 05 '22 at 07:52
  • Do you have any example input? – deHaar Sep 05 '22 at 07:57
  • What is the problem? Does the code fail to compile or doesn't it provide the expected output? – deHaar Sep 05 '22 at 08:07
  • The problem is that the app crash when I use it. So I think that the code is wrong. – user19846644 Sep 08 '22 at 12:46

2 Answers2

1

Your mix of outdated (SimpleDateFormat, 'Date') and modern (LocalDate) APIs is not optimal, I think:

I would use plain java.time here, because…

  • you can obviously use it in your application
  • it has a specific class for datetime Strings of the pattern you have shown in your question: an OffsetDateTime and
  • there's a java.time.Duration which you have tried to use

Here's an example:

fun main(args: Array<String>) {
    // example input, some future datetime
    val input = "2022-12-24T13:22:51.837Z"
    // parse that future datetime
    val offsetDateTime = OffsetDateTime.parse(input)
    // build up a duration between the input and now, use the same class
    val duration = Duration.between(OffsetDateTime.now(), offsetDateTime)
    // get the difference in full days
    val days = duration.toDays()
    // print the result as "days left"
    println("$days days left")
}

Output:

110 days left

If you don't receive a datetime but a date without time (just day of month, month of year and year), then use a LocalDate and calculate the ChronoUnit.DAYS.between(today, futureDate)

fun main(args: Array<String>) {
    // example input, some future date
    val input = "2022-12-24"
    // parse that
    val futureDate = LocalDate.parse(input)
    // get the difference in full days
    val days = ChronoUnit.DAYS.between(LocalDate.now(), futureDate)
    // print the result
    println("$days days left")
}

Output (again):

110 days left
deHaar
  • 17,687
  • 10
  • 38
  • 51
0

Try below code -

        val previousTimeDouble: Double = previousTime.toDouble()
        val nowTimeDouble: Double = System.currentTimeMillis().toDouble()
        val dateNowString: String = dateNow.toString();

        val time: Long = (nowTimeDouble - previousTimeDouble).toLong()

        val difference: String= differenceBetweenTwoTimes(time)

        Log.d("difference", difference)

Function for converting time difference into units -

fun differenceBetweenTwoTimes(time: Long): String {
            var x: Long = time / 1000
            var seconds = x % 60
            x /= 60
            var minutes = x % 60
            x /= 60
            var hours = (x % 24).toInt()
            x /= 24
            var days = x
            return String.format("%02d:%02d:%02d", hours, minutes, seconds)
        }
Aditya Nandardhane
  • 915
  • 1
  • 8
  • 22