Kotlin – Get Current Date/Time

kotlin-get-current-datetime-feature-image

In the tutorial, grokonez will show you how to get current Date/Time by LocalDateTime, LocalDate, LocalTime & ZonedDateTime of java.time package with Kotlin language.

I. Kotlin – Get Current Date/Time

1. Use LocalDateTime

If we want to get both current date & time, we use LocalDateTime:

How to format it? -> We can use DateTimeFormatter

1.1 Formatted by Defined Constants of DateTimeFormatter

1.2 Formatted by Pattern

1.3 Formatted by Locale

2. Use LocalDate & LocalTime

If we just want to get only the current date, we can use LocalDate:

And we can use LocalTime to get only current time:

3. Use ZonedDateTime

We use ZonedDateTime to get the current Date/Time with time zone.

II. Full Sourcecode



By grokonez | January 25, 2018.

Last updated on September 11, 2018.



Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*