You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Kotlin使用DateTimeFormatter格式化日期秒数始终为00问题求助

问题原因

DateTimeFormatter的格式符号大小写是严格区分的,你编写的展示格式串yyyy-MM-dd, HH:MM:SS用错了两个符号:

  • 大写M的含义是「年份中的月份」,只有小写m才代表「小时中的分钟」
  • 大写S的含义是「秒的小数部分(毫秒/微秒等)」,只有小写s才代表「分钟中的秒数」

你用SS作为秒的占位符,而你输入的原始日期字符串没有携带毫秒信息,默认毫秒值为0,因此最终输出的秒位始终显示为00。

解决方法

把展示用的DateTimeFormatter格式串修正为yyyy-MM-dd, HH:mm:ss即可,修正后的完整代码如下:

var date = item?.authDate.toString()
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssz")
val parsedDate = formatter.parse(date)
val displayFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd, HH:mm:ss")
text = displayFormatter.format(parsedDate).toString()

内容的提问来源于stack exchange,提问作者misterios

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.07 13:36:04