datekotlinformatdatetime

Format month of date to string of 3 first letters- Kotlin


I have this date "08/08/2019" and I want it to look like this: "08, Aug 2019", I tried to use when but wondered if there is an easier way to do this? I know it's a bit small question but I tried to find an answer over the internet and I couldn't find this.


Solution

  • first, you need to convert the string to Date object then convert it to your format using the new java.time

    Update

    val firstDate = "08/08/2019"
    val formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy")
    val date = formatter.parse(firstDate)
    val desiredFormat = DateTimeFormatter.ofPattern("dd, MMM yyyy").format(date)
    println(desiredFormat) //08, Aug 2019
    

    old answer

    val firstDate = "08/08/2019"
    val formatter = SimpleDateFormat("dd/MM/yyyy")
    val date = formatter.parse(firstDate)
    val desiredFormat = SimpleDateFormat("dd, MMM yyyy").format(date)
    println(desiredFormat) //08, Aug 2019