1. Overview

Dealing with dates in software development concerns a wide variety of tasks to solve specific problems. One regular task related to this is to get the current date or date/time.

In this tutorial, we’ll explore some techniques to get the current date/time in Kotlin.

2. Implementation

To handle date or date/time values, we’ll take advantage of the support that the LocalDate and LocalDateTime classes provide natively.

2.1. Current LocalDate and LocalDateTime

First, let’s use the static method now() to get the current date:

val current = LocalDate.now()

Similarly, let’s apply the same method to get the current date/time:

val current = LocalDateTime.now()

If we want to get the date in a certain format, we can apply the formatting with the help of the DateTimeFormatter class:

val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")
val current = LocalDateTime.now().format(formatter)

2.2. Using Calendar

The Calendar class provides another approach to getting the current date/time by retrieving the time attribute. Additionally, we can apply the formatting to get a more suitable representation:

val time = Calendar.getInstance().time 
val formatter = SimpleDateFormat("yyyy-MM-dd HH:mm") 
val current = formatter.format(time)

Furthermore, the Calendar class allows us to build the current date/time from its components:

val calendar = Calendar.getInstance()

val current = LocalDateTime.of(
	 calendar.get(Calendar.YEAR),
	 calendar.get(Calendar.MONTH),
	 calendar.get(Calendar.DAY_OF_MONTH),
	 calendar.get(Calendar.HOUR_OF_DAY),
	 calendar.get(Calendar.MINUTE),
	 calendar.get(Calendar.SECOND)
)

2.3. Using java.util.Date

The java.util package contains the Date class, which was used widely before the introduction of the java.time package. To get the current date using the java.util implementation, we simply need to instantiate a new object of the Date class. Additionally, we can apply a format in the same way we used before with DateTimeFormatter:

val formatter = SimpleDateFormat("yyyy-MM-dd")
val date = Date()
val current = formatter.format(date)

3. Current Time as a Timestamp

Epoch-based timestamps are a de-facto standard to record the time of an event in a system. In this section, we’ll explore multiple ways to get the current timestamp in Kotlin.

3.1. Using the java.util.Date Class

Let’s start by creating an instance of the java.util.Date class:

val currentDateTime: java.util.Date = java.util.Date()

Now, we can use the time property of the currentDateTime object to get the timestamp value:

val currentTimestamp: Long = currentDateTime.time

That’s it! We’ve got the timestamp value in the currentTimestamp variable. However, we must note that using the java.util.Date is a legacy approach, and Java still supports this class only for backward compatibility reasons.

3.2. Using the java.time Package

We should move away from the java.util.Date class for all new code, and switch to using classes from the java.time package. In this section, let’s use different classes from this package to get the current timestamp.

First, let’s see how we can get the current timestamp value by using an instance of the java.time.Instant class:

val currentInstant: java.time.Instant = java.time.Instant.now()
val currentTimeStamp: Long = currentInstant.toEpochMilli()

We used the now() method to query the current date and time from the system clock and then used the toEpochMilli() method to get the current timestamp.

Next, let’s create an instance of the ZonedDateTime class and use the toEpochMilli() method to get the current timestamp:

val zoneId: java.time.ZoneId = java.time.ZoneId.systemDefault()
val currentZonedDateTime: java.time.ZonedDateTime = java.time.ZonedDateTime.now(zoneId)
val currentTimestamp: Long = currentZonedDateTime.toInstant().toEpochMilli()

We must note that we’re using the system’s default zone to initialize the currentZonedDateTime variable.

Furthermore, we could also provide a custom ZoneId value to the now() method:

val zoneId: java.time.ZoneId = java.time.ZoneId.of("America/New_York")
val currentZonedDateTime: java.time.ZonedDateTime = java.time.ZonedDateTime.now(zoneId)
val currentTimestamp: Long = currentZonedDateTime.toInstant().toEpochMilli()

In this case, we created the zoneId object using the preferred time zone of “America/New_York” and supplied the zoneId instance to the now() method.

Moving on, let’s also learn how we can get the current timestamp from an instance of the LocalDateTime class:

val localDateTime: java.time.LocalDateTime = java.time.LocalDateTime.now()
val zonedDateTime: java.time.ZonedDateTime = localDateTime.atZone(java.time.ZoneId.of("Asia/Kolkata"))
val currentTimestamp: Long = zonedDateTime.toInstant().toEpochMilli()

It’s imperative to note that the LocalDateTime object lacks any information about the zone, so we must associate it with the local time zone. Further, we need to convert it to an Instant type and access the toEpochMilli() method for getting the timestamp value.

Now, let’s explore another exciting approach to get the timestamp using the OffsetDateTime class. To create an instance of the OffsetDateTime class, we need to provide information about the current date, time, and the time zone offset from GMT:

val currentDate = java.time.LocalDate.now()
val currentTime = java.time.OffsetTime.now()
val currentOffsetDateTime = java.time.OffsetDateTime.of(currentDate, currentTime.toLocalTime(), currentTime.offset)
val currentTimestamp = currentOffsetDateTime.toInstant().toEpochMilli()

Great! We used the LocalDate, OffsetTime, and OffsetDateTime classes to solve our use case.

Finally, let’s check out how to use the systemUTC() method from the Clock class to get the current timestamp:

val currentTimestamp = java.time.Clock.systemUTC()

Perfect! With this, we’ve explored the java.time package extensively to get the current timestamp value.

3.3. Using the Joda-Time Library

Alternatively, we could use the Joda-Time library to solve our use case of getting the current timestamp.

First, we must add the dependency for the joda-time library in the project’s pom.xml file:

<dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time</artifactId>
    <version>2.12.5</version>
</dependency>

Next, let’s see how we can use the millis property of an instance of the Instant class to get the timestamp:

val currentInstant: org.joda.time.Instant = org.joda.time.Instant.now()
val currentTimestamp: Long = currentInstant.millis

Lastly, let’s also learn how we can use the DateTime class instead and get the timestamp value from the millis property:

val currentDateTime: org.joda.time.DateTime = org.joda.time.DateTime.now()
val currentTimestamp: Long = currentDateTime.millis

Great! It looks like we’ve nailed this.

4. Conclusion

In this tutorial, we’ve explored some techniques to generate the current date/time in Kotlin. We saw the implementations using LocalDate, LocalDateTime, and Calendar, and also learned about the support provided by the java.util.Date class.

Furthermore, we learned how to get the current timestamp using the java.util.Date class, java.time package, and the joda-time library.

As always, the complete code for this article is available over on GitHub.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.