In this article, we will see different ways to get the current local date and time in Kotlin.

Using LocalDateTime

Note: Java 8+ is required

You can use the java.time.LocalDateTime class to get the current local Date and Time. If the Java version is less than 8, please skip to the next example.

Following is the example:

import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

fun main() {
    val currentDateTime =
    val dateFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy")
    val timeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss")

    val currentDate = currentDateTime.format(dateFormatter)
    val currentTime = currentDateTime.format(timeFormatter)

    println("Current Date: $currentDate")
    println("Current Time: $currentTime")

Using the java.util.Date

By using the java.util.Date, we can get the current local date and time in the UTC format. To make it readable

We can extract the individual day, month and year from it by using the java.util.Calendar. Using the calendar.get() API, we can extract the YEAR, MONTH, DAY, HOUR, MINUTE & SECONDS.

See the following example for the implementation details.

import java.util.Date
import java.util.Calendar

fun main() {
    val currentDate = Date()
    val calendar = Calendar.getInstance()
    calendar.time = currentDate

    val year = calendar.get(Calendar.YEAR)
    val month = calendar.get(Calendar.MONTH) + 1 
    // Note: Month is zero-based
    val day = calendar.get(Calendar.DAY_OF_MONTH)
    val hour = calendar.get(Calendar.HOUR_OF_DAY)
    val minute = calendar.get(Calendar.MINUTE)
    val second = calendar.get(Calendar.SECOND)

    println("Current Date: $day/$month/$year")
    println("Current Time: $hour:$minute:$second")

Categorized in:

Tagged in: