最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

android - DateTimeFormatter.format() crashes with ArrayIndexOutOfBoundsException - Stack Overflow

programmeradmin3浏览0评论

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
  • can you share the piece of code where you have used this dateTimeFormatterOfBestDateTimePattern? – Jayanta Sarkar Commented Mar 24 at 7:35
  • I've edited the post – okycelt Commented Mar 24 at 7:53
  • I don't think there is problem with dateFormatterOfBestdateTimePattern. As I tried this code and it works fine. Do check the the line number 1107 and 581 in your SourceFile. – Jayanta Sarkar Commented Mar 24 at 9:14
Add a comment  | 

1 Answer 1

Reset to default 0

Updated 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))

发布评论

评论列表(0)

  1. 暂无评论