kotlin date to string code example
Example 1: date to string kotlin
private fun Date.dateToString(format: String): String {
val dateFormatter = SimpleDateFormat(format, Locale.getDefault())
return dateFormatter.format(this)
}
val timestampt = Date()
val dateString = timestamp.dateToString("hh:mm a E dd-MMM")
Example 2: kotlin date to string
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
fun main(args: Array<String>) {
val current = LocalDateTime.now()
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")
val formatted = current.format(formatter)
println("Current Date and Time is: $formatted")
}