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

I'm creating a DateTimeFormatter to format a LocalDate with a pattern from DateFormat.getBestDateT

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744256310a4565415.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信