This article explores different ways to find the current date and time in Kotlin.

1. Using java.util.LocalDateTime class

The standard solution to get the current date-time using the system clock and default time-zone is using the LocalDateTime.now() function.

Download Code

 
To format the date-time, you can specify a formatter to the format() function or use the DateTimeFormatter.ofPattern() function instead to create a custom date-time formatter.

Download Code

2. Using java.util.ZonedDateTime class

The ZonedDateTime class is used to get the time-zone information from the system clock. It can be used as follows:

Download Code

 
You can specify the Zone information to the ZonedDateTime.now() function to get the current date-time in the desired time-zone.

Download Code

 
To format date and time, you can pass the date-time formatter using the DateTimeFormatter.ofPattern() function.

Download Code

3. Using java.util.Date class

Another solution to get the current date and time, with millisecond precision, is using the java.util.Date class.

Download Code

 
To format and parse date with the standard pattern letters, you can use the SimpleDateFormat class.

Download Code

4. Using java.util.Instant class

The Instant class represents an instantaneous point on the time-line. You can use the Instant.now() function to obtain the current instant using the system clock.

Download Code

That’s all about finding the current date and time in Kotlin.