How to get current local date and time in Kotlin

81

How to get current Date (day month and year) and time (hour, minutes and seconds) all in local time in Kotlin?

I tried through LocalDateTime.now() but it is giving me an error saying Call requires API Level 26 (curr min is 21).

How could I get time and date in Kotlin?

This question is tagged with android datetime kotlin

~ Asked on 2017-10-29 23:18:16

The Best Answer is


39

java.util.Calendar.getInstance() represents the current time using the current locale and timezone.

You could also choose to import and use Joda-Time or one of the forks for Android.

~ Answered on 2017-10-30 00:18:10


63

Try this :

 val sdf = SimpleDateFormat("dd/M/yyyy hh:mm:ss")
 val currentDate = sdf.format(Date())
 System.out.println(" C DATE is  "+currentDate)

~ Answered on 2018-05-02 14:44:55


Most Viewed Questions: