Android获取泰文数字格式当前年份的正确方法
获取泰文数字格式的泰国佛历年份
你可以直接利用DateTimeFormatter自定义格式,结合泰文数字区域设置和泰国佛历纪年体系来实现,不需要手动字符转换。以下是具体实现代码:
Kotlin 示例代码
// 初始化带泰文数字的泰语区域设置 val thaiNumberLocale = Locale.forLanguageTag("th-TH-u-nu-thai") // 获取当前时间 val currentDateTime = LocalDateTime.now() // 自定义年份格式化器:只保留年份,使用泰文数字和佛历 val yearFormatter = DateTimeFormatter.ofPattern("yyyy") .withLocale(thaiNumberLocale) .withChronology(ThaiBuddhistChronology.INSTANCE) // 格式化得到泰文数字的佛历年份 val thaiBuddhistYearInThaiDigits = yearFormatter.format(currentDateTime)
代码说明
- 使用
ofPattern("yyyy")指定只格式化年份部分 withLocale(thaiNumberLocale)确保数字显示为泰文数字(如๒๐๒๓)withChronology(ThaiBuddhistChronology.INSTANCE)将公历年份转换为泰国佛历(如2023→2566)
如果需要单独处理公历的泰文数字年份(不转佛历),只需去掉.withChronology(ThaiBuddhistChronology.INSTANCE)即可。
内容的提问来源于stack exchange,提问作者hippietrail
相关产品推荐
相关产品推荐

