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.annotation.SuppressLint
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import androidx.core.os.ConfigurationCompat
|
|
||||||
import me.ash.reader.R
|
import me.ash.reader.R
|
||||||
import java.text.DateFormat
|
import java.text.DateFormat
|
||||||
import java.text.ParsePosition
|
import java.text.ParsePosition
|
||||||
import java.text.SimpleDateFormat
|
import java.text.SimpleDateFormat
|
||||||
import java.util.Calendar
|
import java.util.Calendar
|
||||||
import java.util.Date
|
import java.util.Date
|
||||||
|
import java.util.Locale
|
||||||
|
|
||||||
@SuppressLint("SimpleDateFormat")
|
@SuppressLint("SimpleDateFormat")
|
||||||
object DateFormat {
|
object DateFormat {
|
||||||
|
@ -25,18 +25,18 @@ fun Date.formatAsString(
|
||||||
onlyHourMinute: Boolean? = false,
|
onlyHourMinute: Boolean? = false,
|
||||||
atHourMinute: Boolean? = false,
|
atHourMinute: Boolean? = false,
|
||||||
): String {
|
): String {
|
||||||
val locale = ConfigurationCompat.getLocales(context.resources.configuration)[0]
|
val locale = Locale.getDefault()
|
||||||
val df = DateFormat.getDateInstance(DateFormat.FULL, locale)
|
val df = DateFormat.getDateInstance(DateFormat.FULL, locale)
|
||||||
return when {
|
return when {
|
||||||
onlyHourMinute == true -> {
|
onlyHourMinute == true -> {
|
||||||
SimpleDateFormat("HH:mm", locale).format(this)
|
this.toTimeString(context = context)
|
||||||
}
|
}
|
||||||
|
|
||||||
atHourMinute == true -> {
|
atHourMinute == true -> {
|
||||||
context.getString(
|
context.getString(
|
||||||
R.string.date_at_time,
|
R.string.date_at_time,
|
||||||
df.format(this),
|
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(
|
private fun String.parseToDate(
|
||||||
patterns: Array<String> = arrayOf(
|
patterns: Array<String> = arrayOf(
|
||||||
"yyyy-MM-dd'T'HH:mm:ss'Z'",
|
"yyyy-MM-dd'T'HH:mm:ss'Z'",
|
||||||
|
|
Loading…
Reference in New Issue