Dr. Domenico Giacomo Campanile

Sviluppatore App Web e Mobile

Format date from integer, LocalDate or Calendar to string Kotlin

Format date from integer type

 fun formatDate(date: Int): String {

        return SimpleDateFormat("dd MMMM yyyy")
            .format(Date(date * 1000L))
    }

    fun formatMonthYear(date: Int): String {

        return SimpleDateFormat("MMMM yyyy")
            .format(Date(date * 1000L))
    }

Format date from LocalDate type

 fun formatDate(date: LocalDate): String {

        val formatter = DateTimeFormatter.ofPattern("dd MMMM yyyy")

        return date.format(formatter)
    }

Format date from Calendar type

fun formatDate(date: Calendar): String {
        val formatter = SimpleDateFormat("dd MMMM yyyy")
        return formatter.format(date.time)
    }