Question : kotlin date to string
Answered by : vastemonde
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")
}
// Current Date and Time is: 2021-04-10 10:28:21.052
Source : https://www.datetimeformatter.com/how-to-format-date-time-in-kotlin/ | Last Update : Wed, 28 Apr 21
Question : date to string kotlin
Answered by : tonnie
//create an extension function on a date class which returns a string private fun Date.dateToString(format: String): String { //simple date formatter val dateFormatter = SimpleDateFormat(format, Locale.getDefault()) //return the formatted date string return dateFormatter.format(this) } //call the extension function on a date object val timestampt = Date() val dateString = timestamp.dateToString("hh:mm a E dd-MMM")
Source : | Last Update : Tue, 27 Oct 20