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

Android日期格式化问题:将带时区日期转为指定无时区格式

问题解决:日期格式化时区偏差问题

问题原因

你的代码出现时区偏差的核心原因是错误的时区转换逻辑:

  • 原日期Wed Feb 28 00:00:00 GMT+01:00 2024对应的UTC时间是Tue Feb 27 23:00:00 UTC 2024
  • 你用设置为UTC时区的SimpleDateFormat格式化这个日期,得到的字符串是2024-02-27(UTC时区下此时为27号)
  • 再用同一个UTC时区的SimpleDateFormat解析该字符串,得到的Date对象对应UTC时间2024-02-27 00:00:00
  • 最终输出这个Date时,系统会转换回本地时区(GMT+01:00),也就是2024-02-27 01:00:00 GMT+01:00,和预期结果不符

另外,SimpleDateFormat是线程不安全的类,不推荐在现代Android开发中使用。

解决方案

方案一:使用Java 8+ DateTime API(推荐)

Android从API 26开始原生支持,低版本可通过desugar工具或引入三方库(如ThreeTenABP)兼容。

代码示例:

import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter

fun formatDate(date: ZonedDateTime): String {
    val formatter = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss yyyy")
    return date.format(formatter)
}

// 调用示例:将原始日期字符串转为ZonedDateTime对象
val originalDate = ZonedDateTime.parse(
    "Wed Feb 28 00:00:00 GMT+01:00 2024",
    DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy")
)
val formattedDate = formatDate(originalDate)
// 输出结果:Wed Feb 28 00:00:00 2024

如果原始日期是Date对象,可先转换为ZonedDateTime:

import java.util.Date
import java.time.Instant
import java.time.ZoneId

val date: Date = ... // 你的原始Date对象
val zonedDateTime = date.toInstant().atZone(ZoneId.of("GMT+01:00"))

方案二:修复SimpleDateFormat逻辑(不推荐)

若必须使用旧API,需保证格式化和解析时使用原日期的时区,而非UTC:

import java.text.SimpleDateFormat
import java.util.Locale
import java.util.TimeZone
import java.util.Date

fun formatDate(date: Date): String? {
    return try {
        val locale = Locale("en")
        // 定义目标格式
        val targetFormat = SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy", locale)
        // 设置时区为原日期的时区(GMT+01:00)
        targetFormat.timeZone = TimeZone.getTimeZone("GMT+01:00")
        targetFormat.format(date)
    } catch (e: Exception) {
        null
    }
}

或者直接跳过多余的parse步骤,直接格式化原日期:

val dateFormat = SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy", Locale("en"))
val formatted = dateFormat.format(originalDate)
// 输出结果:Wed Feb 28 00:00:00 2024

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 22:32:51