Working with Dates in Kotlin
Date and time datatypes are widely used in programming. This article is to quickly summarize the commonly used date and time operations in kotlin programming.
Kotlin provides several features to work with dates. Most commonly used classes are java.time.LocalDate, java.time.LocalDateTime, java.util.Calendar and java.util.Date.
The Date class represents a specific point in time, while the Calendar class represents a calendar with its associated time zone. The LocalDate class represents a date without a time zone or time of day. LocalDateTime class represents date and time information.
Current Date and Time
Below example shows how to get current date, time and a specific date.
val currentDate = LocalDate.now()
val specificDate= LocalDate.of(2024, Month.MARCH, 24)
val currentTime = LocalTime.now()
Parsing Dates
In the below examples, we use DateTimeFormatter which tells kotlin how to parse the given date string. Then using LocalDate.parse() function we convert the string to date object.
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
fun main() {
val dateString1 = "24-03-2024"
val dateString2 = "2024-03-24 14:30:00"
val formatter1 = DateTimeFormatter.ofPattern("dd-MM-yyyy")
val formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
val date = LocalDate.parse(dateString1, formatter1)
println(date)
val dateTime = LocalDateTime.parse(dateString2, formatter2)
println(dateTime)
}
Formatting Dates
In the below example we will see how to format the given date object for display purposes. Kotlin provides several formatting options, we can choose it as per our requirement.
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
fun main() {
val date = LocalDate.of(2024, 3, 24)
val formatter = DateTimeFormatter.ofPattern("EEEE, MMMM d, yyyy")
val dateString = date.format(formatter)
println(dateString)
val dateTime = LocalDateTime.now()
val formatter2 = DateTimeFormatter.ofPattern("dd-MM-YYYY HH:mm:ss")
val dateTimeString = dateTime.format(formatter2)
println(dateTimeString)
}
Time Durations
In the below example we will see how to calculate duration between the given 2 dates.
import java.time.Duration
import java.time.LocalDate
fun main() {
val dateToday = LocalDate.of(2024, 3, 24)
val dateNextMonth = LocalDate.of(2024, 4, 25)
val duration = Duration.between(dateToday.atStartOfDay(), dateNextMonth.atStartOfDay())
val days = duration.toDays()
println("$days days")
}
Comparison between Dates
Below example takes 2 dates and checks which date comes first or next or if they are same.
import java.time.LocalDate
fun main() {
val date1 = LocalDate.of(2024, 3, 24)
val date2 = LocalDate.of(2024, 3, 25)
println(date1.isBefore(date2))
println(date1.isAfter(date2))
println(date1.isEqual(date2))
}
Arithmetic Operations on Dates
In the below example, we can see how to add days, weeks, months and Years to given date.
import java.time.LocalDate
fun main() {
val currentDate = LocalDate.now()
val nextDate = currentDate.plusDays(2)
val nextWeek = currentDate.plusWeeks(1)
val nextMonth = currentDate.plusMonths(1)
val nextYear = currentDate.plusYears(1)
println("Current Date - "+currentDate)
println("Next Date - "+nextDate)
println("Next week - "+nextWeek)
println("Next month - "+nextMonth)
println("Next year - "+nextYear)
}
Using Calendar Class
In the below example you can see how to add days and months using calendar class.
import java.util.Calendar
import java.util.Date
fun main() {
val calendar = Calendar.getInstance()
val calendar1 = Calendar.getInstance()
calendar.time = Date()
calendar.add(Calendar.DAY_OF_MONTH, 10)
calendar1.add(Calendar.DAY_OF_WEEK,1)
val newDate = calendar.time
val newDay = calendar1.time
println(newDate)
println(newDay)
}
Conclusion
Kotlin has rich set of classes and functions to work with dates, by understanding these tools we can efficiently work with date and time datatypes.