Kotlin Convert DateTime Into TimeStamp Example

Kotlin Convert DateTime Into TimeStamp Example

Code:
Toast.makeText(
applicationContext,
"" + convertedDateTimeToTimeStamp("2022-04-28 12:50:00", "yyyy-MM-dd HH:mm:ss"),
Toast.LENGTH_SHORT
).show()
Function:
@SuppressLint("SimpleDateFormat")
fun convertedDateTimeToTimeStamp(strdate: String, inputDateFormat: String?): Long {
var longDateTimeStamp: Long = 0
val formatter = SimpleDateFormat(inputDateFormat)
var date: Date? = null
try {
date = formatter.parse(strdate) as Date
longDateTimeStamp = date.time / 1000
} catch (e: ParseException) {
e.printStackTrace()
}
return longDateTimeStamp
}
Output:

Comments