Kotlin Get Current Date Time Example
Code:
Toast.makeText(applicationContext, getCurrentDateTime(), Toast.LENGTH_SHORT).show()
// get current date time
fun getCurrentDateTime(): String {
var strCurrentDate: String
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val current = LocalDateTime.now()
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
val formatted = current.format(formatter)
println("Current Date and Time is: $formatted")
strCurrentDate = formatted.toString()
} else {
val c = Calendar.getInstance()
val df = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
strCurrentDate = df.format(c.time)
}
return strCurrentDate.toString()
}
Comments
Post a Comment