0

I'm looking for a way to convert UTC date to LocalDateTime without limiting to API 26 (Android O) and above. Is there an alternate way to do this that will support API levels below API 26? I've currently got this and it works for devices running 26 and above:

@RequiresApi(Build.VERSION_CODES.O)
override fun convertUtcToLocalDateTime(utcVal: String): LocalDateTime {
    return convertUtcToDate(utcVal).toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime()
}

override fun convertUtcToDate(utcDate: String): Date {
        val formats = arrayListOf<String>(
            "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",
            "yyyy-MM-dd'T'HH:mm:ss.SS'Z'",
            "yyyy-MM-dd'T'HH:mm:ss'Z'",
            "yyyy-MM-dd'T'HH:mm:ss.SSS"
        )

        var date: Date? = null
        formats.forEach {
            if (date == null) {
                try {
                    val parser = SimpleDateFormat(it, Locale.getDefault())
                    parser.timeZone = TimeZone.getTimeZone("UTC")
                    date = parser.parse(utcDate)
                } catch (e: Exception) {
                    date = null
                }
            }
        }

        return date ?: Date()
    }

Sample value that gets sent as utcVal: 2021-09-05T13:55:35.097Z

Please let me know if theres an alternate that supports older APIs as well.

PewK
  • 397
  • 3
  • 8
  • 19
  • Does this answer your question? [Convert String Date to Date in Android (Java/Kotlin) without having to deal with Call requires API level 26](https://stackoverflow.com/questions/68191152/convert-string-date-to-date-in-android-java-kotlin-without-having-to-deal-with). Does [this](https://stackoverflow.com/questions/28745205/cannot-resolve-symbol-java-time-localdate-error-in-android-studio)? – Ole V.V. Jun 15 '22 at 04:45
  • Unfortunately not. That one doesn't address the UTC to LocalDateTime conversion @OleV.V. – PewK Jun 15 '22 at 04:53
  • But you have got the conversion right already? Don’t those two answers explain how to get code using java.time to work on Android API level under 26? I am no Android developer myself. Am I missing something? – Ole V.V. Jun 15 '22 at 04:57
  • What does your `utcVal` look like? Does `convertUtcToDate()` return an old-fashioned `java.util.Date`? Don’t do that. Take the full step and parse into an `Instant` or other modern type from the outset and save that detour. – Ole V.V. Jun 15 '22 at 04:58
  • 1
    Updated the question @OleV.V. – PewK Jun 15 '22 at 05:03
  • Not sure I understand the question, but perhaps [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) is what you're looking for? – Slaw Jun 15 '22 at 08:08
  • While your code holds room for improvement, I believe that the best answer to your question as asked is that [coreLibraryDesugaring](https://developer.android.com/studio/write/java8-support-table) will get your existing code working also on Android devices with API level under 26. – Ole V.V. Jun 15 '22 at 18:40

1 Answers1

0

TLDR: When to use ThreeTenABP

The predecessor to LocalDateTime (JSR-310) is Joda-Time

Although you probably should look at ThreeTenABP which references the Joda-Time Android specific version.

Also be aware of: Differences between Java 8 Date Time API (java.time) and Joda-Time

Morrison Chang
  • 11,691
  • 3
  • 41
  • 77