I'm creating a DateTimeFormatter
to format a LocalDate
with a pattern from DateFormat.getBestDateTimePattern(..)
. This works on all the devices we have in the office, but I'm seeing a lot of crashes in Crashlytics. I've managed to find out the pattern generated by getBestDateTimePattern()
(d 'de' MMMM
) and locale (spa (ESP)
) from the crashes, but this combination works on all our devices, so a pattern/locale might not be the issue. Has anybody come across this?
fun dateTimeFormatterOfBestDateTimePattern(
pattern: String,
locale: Locale = Locale.getDefault()
): DateTimeFormatter {
val datePattern = android.text.format.DateFormat.getBestDateTimePattern(locale, pattern)
return DateTimeFormatter.ofPattern(datePattern)
.withLocale(locale)
.withZone(ZoneId.systemDefault())
}
val date = LocalDate.now()
val formatter = dateTimeFormatterOfBestDateTimePattern("MMMMd")
formatter.format(date)
Fatal Exception: java.lang.ArrayIndexOutOfBoundsException: length=0; index=-1
at j$.time.zone.f.i(SourceFile:1107)
at j$.time.format.A.<init>(SourceFile:581)
at j$.time.format.DateTimeFormatter.format(SourceFile:1794)
I'm creating a DateTimeFormatter
to format a LocalDate
with a pattern from DateFormat.getBestDateTimePattern(..)
. This works on all the devices we have in the office, but I'm seeing a lot of crashes in Crashlytics. I've managed to find out the pattern generated by getBestDateTimePattern()
(d 'de' MMMM
) and locale (spa (ESP)
) from the crashes, but this combination works on all our devices, so a pattern/locale might not be the issue. Has anybody come across this?
fun dateTimeFormatterOfBestDateTimePattern(
pattern: String,
locale: Locale = Locale.getDefault()
): DateTimeFormatter {
val datePattern = android.text.format.DateFormat.getBestDateTimePattern(locale, pattern)
return DateTimeFormatter.ofPattern(datePattern)
.withLocale(locale)
.withZone(ZoneId.systemDefault())
}
val date = LocalDate.now()
val formatter = dateTimeFormatterOfBestDateTimePattern("MMMMd")
formatter.format(date)
Fatal Exception: java.lang.ArrayIndexOutOfBoundsException: length=0; index=-1
at j$.time.zone.f.i(SourceFile:1107)
at j$.time.format.A.<init>(SourceFile:581)
at j$.time.format.DateTimeFormatter.format(SourceFile:1794)
Share
Improve this question
edited Mar 24 at 7:53
okycelt
asked Mar 24 at 7:22
okyceltokycelt
1711 silver badge7 bronze badges
3
|
1 Answer
Reset to default 0Updated code with added -> if conditions and Logs. Please try once
import java.time.LocalDate
import java.time.ZoneId
import java.time.format.DateTimeFormatter
import android.text.format.DateFormat
import android.util.Log
import java.util.*
fun dateTimeFormatterOfBestDateTimePattern(
pattern: String,
locale: Locale = Locale.getDefault()
): DateTimeFormatter {
val datePattern = DateFormat.getBestDateTimePattern(locale, pattern)
Log.d("DatePattern", "Pattern: $datePattern")
if (datePattern.isEmpty()) {
Log.e("DatePatternError", "Generated pattern is empty for locale: $locale. Using fallback pattern.")
return DateTimeFormatter.ofPattern("MMMM d") // Default fallback pattern
.withLocale(locale)
.withZone(ZoneId.systemDefault())
}
return try {
DateTimeFormatter.ofPattern(datePattern)
.withLocale(locale)
.withZone(ZoneId.systemDefault())
} catch (e: Exception) {
Log.e("DatePatternError", "Failed to create DateTimeFormatter with pattern: $datePattern", e)
DateTimeFormatter.ofPattern("MMMM d")
.withLocale(locale)
.withZone(ZoneId.systemDefault())
}
}
// Usage example
val date = LocalDate.now()
val formatter = dateTimeFormatterOfBestDateTimePattern("MMMMd", Locale("es", "ES"))
println(formatter.format(date))
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744256310a4565415.html
dateTimeFormatterOfBestDateTimePattern
? – Jayanta Sarkar Commented Mar 24 at 7:35dateFormatterOfBestdateTimePattern
. As I tried this code and it works fine. Do check the the line number 1107 and 581 in yourSourceFile
. – Jayanta Sarkar Commented Mar 24 at 9:14