Working with Dates in Android Android 01.03.2020

Kotlin doesn't have its own date and time implementation and uses Java classes for date and time. Unfortunately, Java's implementation of date and time has changed multiple times in the past. Many of the early attempts weren't very successful, up to the point where third-party libraries were used instead.

To format dates in an Android application, you must keep in mind that dates formatted using the Android SDK take into account the locale.

Existed classes

Here is a list of the many classes involved in handling and formatting dates :

  • java.util.Date. Represents a single date and compares dates. In Java, the date object also includes the time, so if you only need the date the time should always be the same (for example midnight) to make sure comparisons will return the expected result.
  • java.util.Calendar. Extracts the data for the day, month and year from dates and handles mathematical operations between dates.
  • LocalDate, LocalTime, and LocalDateTime (from API level 26). Since Java 8, the LocalDateTime class and its variants are used exclusively for date and/or time. When we compare it to the Calendar class, it's immutable. Which means, simply put, that we can use it when working with threads (more on this in later Kotlin courses). It also provides a fluent interface which is sometimes referred to as method chaining.
  • Joda-Time. The unsuccessful attempts of implementing date and time in Java resulted in a need for a high-quality replacement for built-in classes. The Joda-Time library became quite popular at this point in time. The new Java 8 date API was inspired, in many ways, by this library and even uses the same concepts. One might even say that Joda-Time might be more powerful and of higher quality. However, I suggest that you keep using the standard LocalDateTime class and avoid unnecessary dependencies on third-party components.
  • android.text.format.DateFormat. Gets the date and time format according the current locale of the device. The format is returned as a java.text.Format that can be used with the Java format classes.
  • java.text.DateFormat. Represents a date format. It should not be used directly since it does not manage the Android locale.
  • java.text.SimpleDateFormat. Derived from java.text.DateFormat, format a date according to the specified format.

How to convert timestamp to formatted date

val cal = Calendar.getInstance(Locale.ENGLISH)
cal.timeInMillis(timestampInMilli)
val date = DateFormat.format("dd-MM-yyyy hh:mm:ss", cal).toString()

Add field date to current date

fun Date.add(field: Int, amount: Int): Date {
    Calendar.getInstance().apply {
        time = this@add
        add(field, amount)
        return time
    }
}

fun Date.addYears(years: Int): Date{
    return add(Calendar.YEAR, years)
}
fun Date.addMonths(months: Int): Date {
    return add(Calendar.MONTH, months)
}
fun Date.addDays(days: Int): Date{
    return add(Calendar.DAY_OF_MONTH, days)
}
fun Date.addHours(hours: Int): Date{
    return add(Calendar.HOUR_OF_DAY, hours)
}
fun Date.addMinutes(minutes: Int): Date{
    return add(Calendar.MINUTE, minutes)
}
fun Date.addSeconds(seconds: Int): Date{
    return add(Calendar.SECOND, seconds)
}