mirror of https://github.com/Ashinch/ReadYou.git
feat(ui): format time with system locale default (#617)
This commit is contained in:
parent
5b51e0bff7
commit
44752b8e7a
|
@ -2,13 +2,13 @@ package me.ash.reader.ui.ext
|
|||
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.Context
|
||||
import androidx.core.os.ConfigurationCompat
|
||||
import me.ash.reader.R
|
||||
import java.text.DateFormat
|
||||
import java.text.ParsePosition
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.Calendar
|
||||
import java.util.Date
|
||||
import java.util.Locale
|
||||
|
||||
@SuppressLint("SimpleDateFormat")
|
||||
object DateFormat {
|
||||
|
@ -25,18 +25,18 @@ fun Date.formatAsString(
|
|||
onlyHourMinute: Boolean? = false,
|
||||
atHourMinute: Boolean? = false,
|
||||
): String {
|
||||
val locale = ConfigurationCompat.getLocales(context.resources.configuration)[0]
|
||||
val locale = Locale.getDefault()
|
||||
val df = DateFormat.getDateInstance(DateFormat.FULL, locale)
|
||||
return when {
|
||||
onlyHourMinute == true -> {
|
||||
SimpleDateFormat("HH:mm", locale).format(this)
|
||||
this.toTimeString(context = context)
|
||||
}
|
||||
|
||||
atHourMinute == true -> {
|
||||
context.getString(
|
||||
R.string.date_at_time,
|
||||
df.format(this),
|
||||
SimpleDateFormat("HH:mm", locale).format(this),
|
||||
this.toTimeString(context = context),
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -59,6 +59,10 @@ fun Date.formatAsString(
|
|||
}
|
||||
}
|
||||
|
||||
private fun Date.toTimeString(context: Context): String =
|
||||
android.text.format.DateFormat.getTimeFormat(context).format(this)
|
||||
|
||||
|
||||
private fun String.parseToDate(
|
||||
patterns: Array<String> = arrayOf(
|
||||
"yyyy-MM-dd'T'HH:mm:ss'Z'",
|
||||
|
|
Loading…
Reference in New Issue