Android:将时间戳转换为本地时间格式

一、效果图

图1,中国的时间格式

图2,美国的时间格式

二、StringUtil.kt代码

bash 复制代码
import java.text.DateFormat
import java.text.SimpleDateFormat
import java.util.*
object StringUtil {

    fun formatTimestamp(currentTime: Long): String {
        var sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
        return sdf.format(Date(currentTime))
    }

    fun formatDateTimeLocale(
        timeMillis: Long?,
        dateStyle: Int = DateFormat.SHORT,
        timeStyle: Int = DateFormat.SHORT
    ): String {
        if (timeMillis == null || timeMillis <= 0L) {
            return ""
        }
        try {
            val locale = Locale.getDefault()
            val dateFormat = DateFormat.getDateInstance(dateStyle, locale)
            val timeFormat = DateFormat.getTimeInstance(timeStyle, locale)
            val calendar: Calendar = Calendar.getInstance(locale)
            calendar.time = Date(timeMillis)
            val formattedDate = dateFormat.format(calendar.time)
            var formattedTime = timeFormat.format(calendar.time)
            return "$formattedDate $formattedTime"
        } catch (e: Exception) {
      
        }
        return ""
    }
}
相关推荐
峥嵘life2 分钟前
Android16 系统 APEX 模块调试总结
android·大数据·开发语言
亘元有量-流量变现8 分钟前
2026安卓ASO增量攻略:摆脱iOS思维依赖,吃透多商店自然免费流量
android·aso优化·亘元有量·方糖试玩
一航jason39 分钟前
安卓车机端 AIOS 技术生态全景
android·人工智能·ai·ai编程·ai-native
深念Y1 小时前
ZTE B860AV1.1-T2 机顶盒改造记录:从 Android 到 Linux 服务器
android·linux·运维·服务器·boot·cma·机顶盒
淡淡的香烟2 小时前
Android中tcp的通信
android·物联网·tcp/ip
Escalating_xu3 小时前
【C++入门基础(下)】默认参数、函数重载、引用、inline 与 nullptr
android·c++·redis
wen_zhufeng14 小时前
用 vLLM 加速 TTS 推理:通用改造指南
android·vllm
长友cy15 小时前
windows 搭建Qt编译Android 应用程序,快速搭建
android·windows
智驭未来掌门人16 小时前
Android Kotlin 开发避坑指南:从语言特性到工程化实践
android
码农coding16 小时前
android12 ViewRootImpl分析
android