首页 > 解决方案 > 如何在 Kotlin 中使用“android.text.format.DateFormat”?

问题描述


    fun bind(crime: Crime){
            this.crime = crime
            titleTextView.text = this.crime.title
            val df : DateFormat = DateFormat.getInstance().format(DateFormat.LONG, DateFormat.MEDIUM)
            dateTextView.text = df.format(this.crime.date.toString())
            solvedImageView.visibility = if (crime.isSolved){
                View.VISIBLE
            }else{
                View.GONE
            }
        }

如何在 Kotlin 中使用日期格式库?

我想将此日期格式“Sun Jul 25 00:00:00 GMT+09:00 2021”更改为“Sun, Jul, 25, 2021”。

标签: androidkotlin

解决方案


您可以简单地使用以下内容:

val formattedDate = SimpleDateFormat("EE, MMM, dd, yyyy", Locale.US)
val date = formattedDate .parse(this.crime.date.toString())

推荐阅读