Home > database >  Android Kotlin Convert date with Extensions file
Android Kotlin Convert date with Extensions file

Time:10-20

I need to convert string date, which comes from api to Thu. 23 Sept 2021. From Api comes many date and I want all of them to be converted. (With Extensions File) How to do it?

CodePudding user response:

use SimpleDateFormat,

val date = SimpleDateFormat("dd/MM/yy").parse("31/11/2021");

This will produce an output that you desired.

ie, Thu Dec 31 00:00:00 IST 1998

CodePudding user response:

You can use this code snippet as kotlin extension.

fun Date.convertDate(dateString:String) : String{
     val inFormat = SimpleDateFormat("dd/MM/yy")
     var date: Date? = null
     try {
        date = inFormat.parse(dateString)
      } catch (e: ParseException) {
        e.printStackTrace()
      }
     val outFormat = SimpleDateFormat("dd MM, YYYY")

    return outFormat.format(date)

}

enter image description here

  • Related