How to convert date string to timestamp in Kotlin?

Since JDK 8 you can do:

val l = LocalDate.parse("14-02-2018", DateTimeFormatter.ofPattern("dd-MM-yyyy"))

val unix = l.atStartOfDay(ZoneId.systemDefault()).toInstant().epochSecond

Note that the example uses your system's default timezone.


use this to convert the date string to UNIX timestamp

val date = SimpleDateFormat("dd-MM-yyyy").parse("14-02-2018")
println(date.time)

For backward compatibility use ThreeTenABP library but a better solution from Android itself will be enabling support https://developer.android.com/studio/write/java8-support#library-desugaring

Once enabled you can now parse String date to any valid format pattern

LocalDate.parse(date, DateTimeFormatter.ISO_LOCAL_DATE_TIME).format(
        DateTimeFormatter.ofPattern("MMM. dd, yyyy")