Find date without time in Kotlin
This article explores different ways to find the date without time in Kotlin.
1. Using java.util.LocalDate class
The standard class for representing a date without a time is the LocalDate from the java.time package. To get the current date using the system clock and default time-zone, you can use the static function now().
|
1 2 3 4 5 6 |
import java.time.LocalDate fun main() { val date = LocalDate.now() println(date) } |
Output (will vary):
2017-01-01
To get the current date in the desired time zone, you can pass the Zone information to the LocalDate.now() function.
|
1 2 3 4 5 6 7 |
import java.time.LocalDate import java.time.ZoneId fun main() { val date = LocalDate.now(ZoneId.of("Europe/Paris")) println(date) } |
Output (will vary):
2017-01-01
2. Using java.util.Date class
To parse a date without time information in the specified format, you can use the DateFormat class. It can be used as follows with the Date object:
|
1 2 3 4 5 6 7 8 |
import java.text.SimpleDateFormat import java.util.* fun main() { val formatter = SimpleDateFormat("MM-dd-yyyy") val date = formatter.format(Date()) println(date) } |
Output (will vary):
01-01-2017
That’s all about finding the date without 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 :)