Kotlin Android / Java String DateTime Format, API21

前端 未结 3 1280
死守一世寂寞
死守一世寂寞 2020-12-15 05:46

I want convert string datetime to formatted string. e.g \"2018-12-14T09:55:00\" to \"14.12.2018 09:55\" as String => Textview.text

how can I do this with kotlin or

3条回答
  •  不知归路
    2020-12-15 06:26

    Kotlin API levels 26 or greater:

    val parsedDate = LocalDateTime.parse("2018-12-14T09:55:00", DateTimeFormatter.ISO_DATE_TIME)
    val formattedDate = parsedDate.format(DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm"))
    

    Below API levels 26:

    val parser =  SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss")
    val formatter = SimpleDateFormat("dd.MM.yyyy HH:mm")
    val formattedDate = formatter.format(parser.parse("2018-12-14T09:55:00"))
    

提交回复
热议问题