date to string kotlin code example

Example 1: date to string kotlin

//create an extension function on a date class which returns a string
    private fun Date.dateToString(format: String): String {
    //simple date formatter
        val dateFormatter = SimpleDateFormat(format, Locale.getDefault())
        
        //return the formatted date string
        return dateFormatter.format(this)
    }
    
    
    //call the extension function on a date object
    val timestampt = Date()
    val dateString = timestamp.dateToString("hh:mm a E dd-MMM")

Example 2: date to string kotlin

//create an extension function on a date class which returns a string
    private fun Date.dateToString(format: String): String {
    //simple date formatter
        val dateFormatter = SimpleDateFormat(format, Locale.getDefault())
        
        //return the formatted date string
        return dateFormatter.format(this)
    }
    
    
    //call the extension function on a date object
    val timestampt = Date()
    val dateString = timestamp.dateToString("hh:mm a E dd-MMM")

Example 3: date to string kotlin

//create an extension function on a date class which returns a string
    private fun Date.dateToString(format: String): String {
    //simple date formatter
        val dateFormatter = SimpleDateFormat(format, Locale.getDefault())
        
        //return the formatted date string
        return dateFormatter.format(this)
    }
    
    
    //call the extension function on a date object
    val timestampt = Date()
    val dateString = timestamp.dateToString("hh:mm a E dd-MMM")