This article explores different ways to find the number of seconds elapsed since the Unix epoch in Kotlin. The Unix epoch (or Unix time or Epoch time or Posix time) is the number of seconds that have elapsed since January 1, 1970, 00:00:00 UTC.

1. Using System.currentTimeMillis() function

The simplest way is probably using the System.currentTimeMillis() function that returns the difference between the current time and Unix epoch in “milliseconds”. This is demonstrated below:

Download Code

Output (will vary):

1640882619680

 
To get the number of “seconds” elapsed since the Unix epoch, we can simply divide the return value of the System.currentTimeMillis() function by 1000.

Download Code

Output (will vary):

1640882633

2. Using Instant.toEpochMilli() function

The java.time.Instant class represents an instantaneous point on the time-line. We can get the current instant using the Instant.now() function and convert this instant to the number of seconds from the Unix epoch with epochSecond.

Download Code

Output (will vary):

1640882923

 
Additionally, we can convert the current instant to the number of milliseconds from the epoch using the toEpochMilli() function.

Download Code

Output (will vary):

1640882923772

That’s all about finding the number of seconds elapsed since the Unix epoch in Kotlin.