how to get time duration from file kotlin

To get the time duration from a file in Kotlin, you can follow these steps:

  1. Import the necessary classes:
import java.io.File
import java.util.Date
  1. Create a File object with the path to the file you want to get the duration from:
val file = File("path/to/file.mp4")

Replace "path/to/file.mp4" with the actual path to your file.

  1. Use the lastModified() method of the File object to get the last modified timestamp of the file:
val lastModified = file.lastModified()

This method returns the time in milliseconds since the Unix epoch (January 1, 1970 00:00:00 UTC).

  1. Create a Date object using the lastModified timestamp:
val lastModifiedDate = Date(lastModified)
  1. Create a Date object for the current time:
val currentTime = Date()
  1. Calculate the duration between the current time and the last modified time:
val durationInMillis = currentTime.time - lastModifiedDate.time

This will give you the duration in milliseconds.

  1. Convert the duration to the desired format (e.g., seconds, minutes, hours) if needed.

That's it! You now have the time duration between the last modified time of the file and the current time.