Find current date and time in Kotlin
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.
1 2 3 4 5 6 7 |
import java.time.LocalDateTime fun main() { val datetime = LocalDateTime.now() println(datetime) // 2017-01-01T22:27:06.006276200 } |
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.
1 2 3 4 5 6 7 8 9 |
import java.time.LocalDateTime import java.time.format.DateTimeFormatter fun main() { val dateTime = LocalDateTime.now() .format(DateTimeFormatter.ofPattern("MMM dd yyyy, hh:mm:ss a")) println(dateTime) // Jan 01 2017, 10:27:41 pm } |
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:
1 2 3 4 5 6 7 |
import java.time.ZonedDateTime fun main() { val datetime = ZonedDateTime.now() println(datetime) // 2017-01-01T22:27:55.937761200+05:30[Etc/UTC] } |
You can specify the Zone information to the ZonedDateTime.now()
function to get the current date-time in the desired time-zone.
1 2 3 4 5 6 7 8 9 |
import java.time.ZoneId import java.time.ZonedDateTime fun main() { val dateTime = ZonedDateTime.now(ZoneId.of("America/Chicago")) // 2017-01-01T10:58:17.691405100-06:00[America/Chicago] println(dateTime) } |
To format date and time, you can pass the date-time formatter using the DateTimeFormatter.ofPattern()
function.
1 2 3 4 5 6 7 8 9 10 |
import java.time.ZoneId import java.time.ZonedDateTime import java.time.format.DateTimeFormatter fun main() { val timestamp = ZonedDateTime.now(ZoneId.of("America/Sao_Paulo")) .format(DateTimeFormatter.ofPattern("MM.dd.yyy hh.mm.ss a")) println(timestamp) // 01.01.2017 01.59.13 pm } |
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.
1 2 3 4 5 6 7 |
import java.util.Date fun main() { val date = Date() println(date) // Sat Jan 01 22:24:51 EST 2017 } |
To format and parse date with the standard pattern letters, you can use the SimpleDateFormat
class.
1 2 3 4 5 6 7 8 9 |
import java.text.SimpleDateFormat import java.util.* fun main() { val dateFormat = SimpleDateFormat("d MMM yyyy, EEE, HH:mm:ss z") val date = dateFormat.format(Date()) println(date) // 1 Jan 2017, Sat, 22:26:02 EST } |
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.
1 2 3 4 5 6 7 |
import java.time.Instant fun main() { val instant = Instant.now() println(instant) // 2017-01-01T16:59:30.663439800Z } |
That’s all about finding the current date and time in Kotlin.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)