【发布时间】:2021-10-19 10:45:23
【问题描述】:
我需要将String 格式的日期转换为星期四。 2021 年 9 月 23 日。,来自 Api。
Api 发送了许多日期,我希望将它们全部转换。 (带扩展文件)
怎么做?
【问题讨论】:
-
使用gson库,可以将大部分(json API date)日期格式转换为java日期
我需要将String 格式的日期转换为星期四。 2021 年 9 月 23 日。,来自 Api。
Api 发送了许多日期,我希望将它们全部转换。 (带扩展文件)
怎么做?
【问题讨论】:
使用SimpleDateFormat,
val date = SimpleDateFormat("dd/MM/yy").parse("31/11/2021");
这将产生您想要的输出。
即,1998 年 12 月 31 日星期四 00:00:00 IST
【讨论】:
您可以将此代码 sn-p 用作 kotlin 扩展。
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)
}
【讨论】: