From 73ad70a8ec69e1a643edf5f37a1f6ba238132cc2 Mon Sep 17 00:00:00 2001 From: Yiqun Zhang Date: Sun, 25 Feb 2024 12:54:53 +0800 Subject: [PATCH] :bug: Fix getDateFormat method --- .../kotlin/com/clipevery/utils/DateUtils.kt | 22 ++++--------------- .../kotlin/com/clipevery/i18n/I18n.kt | 18 ++++++++++++++- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/composeApp/src/commonMain/kotlin/com/clipevery/utils/DateUtils.kt b/composeApp/src/commonMain/kotlin/com/clipevery/utils/DateUtils.kt index cddbe10b7..5e7930992 100644 --- a/composeApp/src/commonMain/kotlin/com/clipevery/utils/DateUtils.kt +++ b/composeApp/src/commonMain/kotlin/com/clipevery/utils/DateUtils.kt @@ -1,7 +1,7 @@ package com.clipevery.utils +import androidx.compose.runtime.remember import io.realm.kotlin.types.RealmInstant -import java.text.SimpleDateFormat import java.time.Instant import java.time.LocalDateTime import java.time.ZoneId @@ -49,24 +49,10 @@ object DateUtils { return dateFormatter.format(LocalDateTime.now()) } - fun getDateFormat(date: LocalDateTime, language: String): String { - val locale = when (language) { - "zh" -> Locale.SIMPLIFIED_CHINESE - "en" -> Locale.US - "jp" -> Locale.JAPAN - "es" -> Locale("es", "ES") - else -> Locale.getDefault() + fun getDateText(date: LocalDateTime, pattern: String, locale: Locale): String { + val formatter = remember(pattern, locale) { + DateTimeFormatter.ofPattern(pattern, locale) } - - val pattern = when (language) { - "en" -> "MM/dd/yyyy" - "es" -> "dd/MM/yyyy" - "jp" -> "yyyy/MM/dd" - "zh" -> "yyyy年MM月dd日" - else -> "MM/dd/yyyy" - } - - val formatter = SimpleDateFormat(pattern, locale) return formatter.format(date) } diff --git a/composeApp/src/desktopMain/kotlin/com/clipevery/i18n/I18n.kt b/composeApp/src/desktopMain/kotlin/com/clipevery/i18n/I18n.kt index d569fa511..fdd1d7b34 100644 --- a/composeApp/src/desktopMain/kotlin/com/clipevery/i18n/I18n.kt +++ b/composeApp/src/desktopMain/kotlin/com/clipevery/i18n/I18n.kt @@ -10,6 +10,7 @@ import java.io.FileNotFoundException import java.io.InputStreamReader import java.nio.charset.StandardCharsets import java.time.LocalDateTime +import java.util.Locale import java.util.Properties import java.util.concurrent.ConcurrentHashMap @@ -102,7 +103,22 @@ class CopywriterImpl(private val language: String) : Copywriter { } override fun getDate(date: LocalDateTime): String { - return DateUtils.getDateFormat(date, language) + val locale = when (language) { + "zh" -> Locale.SIMPLIFIED_CHINESE + "en" -> Locale.US + "jp" -> Locale.JAPAN + "es" -> Locale("es", "ES") + else -> Locale.getDefault() + } + + val pattern = when (language) { + "en" -> "MM/dd/yyyy" + "es" -> "dd/MM/yyyy" + "jp" -> "yyyy/MM/dd" + "zh" -> "yyyy年MM月dd日" + else -> "MM/dd/yyyy" + } + return DateUtils.getDateText(date, pattern, locale) } override fun getAbridge(): String {