Date and time in Android studio (Kotlin language)
For Date
val currentDate: String = SimpleDateFormat("dd/MM/yyyy", Locale.getDefault()).format(Date())
For Time
val currentTime: String = SimpleDateFormat("HH:mm:ss", Locale.getDefault()).format(Date())
Use this, handle versions lower than android Oreo(O), using a SimpleDateFormater
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val current = LocalDateTime.now()
val formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy. HH:mm:ss")
var answer: String = current.format(formatter)
Log.d("answer",answer)
} else {
var date = Date()
val formatter = SimpleDateFormat("MMM dd yyyy HH:mma")
val answer: String = formatter.format(date)
Log.d("answer",answer)
}
Check this useful thread with similar question.
In a nutshell, you can use adaptation of the JSR-310(new Data/Time API) for Android.
https://github.com/JakeWharton/ThreeTenABP
Then you can use LocalDateTime
in the application.