【问题标题】:Parse date in specific format using Thai Calendar in Android在 Android 中使用泰语日历以特定格式解析日期
【发布时间】:2020-02-20 16:19:22
【问题描述】:

对于 Android 应用,我需要以泰语本地格式解析日期(例如,2019 年应返回为 2562)。我不明白我该怎么做。当前使用SimpleDateFormatter 以默认本地格式解析日期。

fun adjustTimePattern(dateString: String, oldPattern: String, newPattern: String): String? {
    val dateFormat = SimpleDateFormat(oldPattern, Locale.getDefault())
    dateFormat.timeZone = TimeZone.getTimeZone("UTC")
    return try {
        val calendar = Calendar.getInstance()
        calendar.time = dateFormat.parse(dateString)
        val newFormat = SimpleDateFormat(newPattern, Locale.getDefault())
        newFormat.format(calendar.time)
    } catch (e: ParseException) {
        return null
    }
}

尝试硬编码语言环境的值(使用 Locale("th", "TH") 而不是 Locale.getDefault()),但由于 SimpleDateFormat 类使用公历本身,所以很幸运。

也尝试过使用LocalDate & DateTimeFormatter,但年份值没有任何变化。

fun formatTime(pattern: String): String {
    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val formatter = DateTimeFormatter.ofPattern(pattern, Locale("th", "TH"))
        LocalDate.now().format(formatter)
    } else {
        ""
    }
}

.

谁能帮我解决这个问题?

【问题讨论】:

  • 您应该更喜欢java.time, the modern Java date and time API 中的课程。但是 LocalDate 本质上是 ISO/proleptic 公历中的日期,因此不适用于泰语日期。答案显示了一个我认为很棒的解决方案。

标签: java android kotlin localdate thai


【解决方案1】:

ThaiBuddhistDate类

您可以使用java.time.chrono.ThaiBuddhistDate 来完成此操作,请参见以下示例:

public static void main(String[] args) throws ParseException {
    ThaiBuddhistDate tbd = ThaiBuddhistDate.now(ZoneId.systemDefault());
    System.out.println(tbd.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
}

我在系统上得到的输出是

2562-10-24

大部分 java.time 功能在 ThreeTen-Backport 项目中向后移植到 Java 6 和 Java 7。进一步适用于ThreeTenABP 中的早期 Android (How to use ThreeTenABP…。

【讨论】:

  • @SudipPodder 是的,请参阅this question。您基本上必须使用支持库,而这个问题是关于如何在 Android 中使用它。
  • 肯定会试一试。如果可行,会将您的答案标记为已接受的答案。您可以编辑您的答案并添加评论链接以及对 API 级别
  • @BasilBourque OP 已经这样做了,我删除了 Android 标签,因为我认为它只是关于 java.time。如果可以在一个问题下方写超过 5 个标签,我就不会删除该标签。
  • 我在问题的开头添加了对 Android 的提及。
  • @SudipPodder 只要您在 1941 年(gregorian)之前没有几年,这个解决方案(基于 ThreetenABP)就可以正常工作。
【解决方案2】:

因为您已经在使用java.util.Calendar 和java.text.SimpleDateFormat。

如果您可以在实例化日历时更改语言环境,这应该可以:

val thaiCalendar = Calendar.getInstance(Locale("th", "TH"))
println(thaiCalendar.get(Calendar.YEAR)) // prints 2562

但是,如果您只是想更改格式,这对我有用:

val notThaiCalendar = Calendar.getInstance()
val thaiFormatter = SimpleDateFormat("yyyy-MM-dd", Locale("th", "TH"))
println(thaiFormatter.format(notThaiCalendar.time)) // prints 2562-10-24

编辑

此解决方案未在 Android 上测试

【讨论】:

  • 仅供参考,存在严重缺陷的日期时间类,例如 java.util.Date、java.util.Calendar 和 java.text.SimpleDateFormat 现在是 legacy,被 Java 8 及更高版本中内置的 java.time 类所取代.
  • 我知道,但他在方法adjustTimePattern的代码中使用了这些类
  • 将检查它@DiegoMagdaleno
  • Android(在大多数移动平台上)与旧 Java 的 Calendar 实现不同,尤其是缺少佛历,所以这个答案可能不起作用。
  • @MenoHochschild 我真的没有在 Android 上测试它。我不知道有这样的区别。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-20
  • 1970-01-01
  • 2020-05-25
相关资源
最近更新 更多