Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get current local date and time in Kotlin

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?

like image 367
rgoncalv Avatar asked Oct 29 '17 23:10

rgoncalv


People also ask

How do I get the current date and time in Kotlin?

This example demonstrates how to get the current time and date in an Android app Kotlin. Step 1 − Create a new project in Android Studio, go to File ⇉ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How do I find my local date and time in Kotlin?

To get local formatting use getDateInstance() , getDateTimeInstance() , or getTimeInstance() , or use new SimpleDateFormat(String template, Locale locale) with for example Locale.US for ASCII dates.

How can I get current date in Android?

getInstance(). getTime(); System. out. println("Current time => " + c); SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy"); String formattedDate = df.


1 Answers

Try this :

 val sdf = SimpleDateFormat("dd/M/yyyy hh:mm:ss")  val currentDate = sdf.format(Date())  System.out.println(" C DATE is  "+currentDate) 
like image 141
Shan Mk Avatar answered Sep 19 '22 06:09

Shan Mk