-1

I have tried these format but none worked.

  1. yyyy-MM-dd'T'HH:mm:ss'Z'
  2. yyyy-MM-dd'T'HH:mm:ssZ
  3. yyyy-MM-dd'T'HH:mm:ssZZ
  4. yyyy-MM-dd'T'HH:mm:ss

Also tried "ZonedDateTime", but it is not available below Android O.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • 1
    Does this answer your question? [Correct way to parse Date in Kotlin Android (Minimum Android Version 21). My parse is not working](https://stackoverflow.com/questions/69987822/correct-way-to-parse-date-in-kotlin-android-minimum-android-version-21-my-par) – Ivo Aug 01 '22 at 11:14
  • @Ivo tried this too. But not working. In my case there is no "milisec" – Akanksha Paul Aug 01 '22 at 11:16
  • Welcome to Stack Overflow. We love to help with code that is not working, only we need a [mre], please? We also need to know what you mean by *none worked*, please. Please paste any wrong result and/or any error message verbatim into the question. – Ole V.V. Aug 01 '22 at 15:02
  • Answer [here](https://stackoverflow.com/questions/2597083/illegal-pattern-character-t-when-parsing-a-date-string-to-java-util-date) and [here](https://stackoverflow.com/questions/3941357/iso-8601-string-to-date-time-object-in-android), For pattern 2 [here](https://stackoverflow.com/questions/20168647/java-text-parseexception-unparseable-date-yyyy-mm-ddthhmmss-sssz-simple). Don’t worry about not having millis, just adapt any pattern to not have. For Android below O [here](https://stackoverflow.com/questions/68191152/convert-string-date-to-date-in-android-java-kotlin-without-having-to-deal-with) – Ole V.V. Aug 01 '22 at 15:11

2 Answers2

2

If your minSDK is 25 or lower you have to use Java 8+ API desugaring support to be able to use the java.time package from Java 8.

With that enabled you can simply use e.g.

OffsetDateTime.parse("2022-07-18T08:24:18Z")
ZonedDateTime.parse("2022-07-18T08:24:18Z")

(you can find many resources about the differences of these date formats).

G00fY
  • 4,545
  • 1
  • 22
  • 26
0

You can do it like this

fun parseDate(
        inputDateString: String?,
        inputDateFormat: SimpleDateFormat,
        outputDateFormat: SimpleDateFormat
    ): String? {
        var date: Date? = null
        var outputDateString: String? = null
        try {
            date = inputDateFormat.parse(inputDateString)
            outputDateString = outputDateFormat.format(date)
        } catch (e: ParseException) {
            e.printStackTrace()
        }
        return outputDateString
    }
  • inputString will be your Date, ex:"2022-07-18T08:24:18Z"
  • inputDateFormat : SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US)
  • outputDateFormat : in whichever format you want to show the date

I hope you get your answer from this

primo
  • 1,340
  • 3
  • 12
  • 40
  • Already mentioned in question. This format is not working – Akanksha Paul Aug 01 '22 at 12:57
  • I tried and tested it. Use my parseDate method with it. – primo Aug 01 '22 at 13:10
  • Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. We have so much better in [`java.time`, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. Yes, you can use it on Android. For older Android look into [desugaring](https://developer.android.com/studio/write/java8-support-table). – Ole V.V. Aug 01 '22 at 15:14
  • I believe it will give 08:24:18 in my own time zone where 08:24:18 UTC was intended, so not correct (have not tested). – Ole V.V. Aug 03 '22 at 03:49